2013-04-24 64 views
0

我剛開始工作的地鐵應用程序,我面臨的問題是調度不更新用戶界面。我的代碼如下,請讓我知道是什麼問題?Metro UI不更新

public class Test : DependencyObject 
{ 

    public static readonly DependencyProperty CurrentItemProperty = 
     DependencyProperty.Register("NameOfPerson", typeof(string), typeof(Test), null); 

    public String NameOfPerson 
    { 
     get 
     { 
      return (string)GetValue(CurrentItemProperty); 
     } 
     set 
     { 
      runmethod(value); 
     } 
    } 

    public async void runmethod(String text) 
    { 
     await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,() => 
     { 
      SetValue(CurrentItemProperty, text); 
     } 
     ); 
    } 
} 

在主頁我有一個事件按鈕,當火災更新文本框時點擊。

private void Button_Click_2(object sender, RoutedEventArgs e) 
    { 
     Test t = new Test(); 
     t.NameOfPerson = "Hello Umar"; 
    } 

MainPage.xaml中像這樣

<Page 
    x:Class="TestApplication.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:TestApplication" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d"> 

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> 
     <Button Content="Button" HorizontalAlignment="Left" Margin="207,187,0,0" VerticalAlignment="Top" Height="80" Width="255" Click="Button_Click_2"/> 
     <TextBox x:Name="textB" Text="{Binding NameOfPerson}" HorizontalAlignment="Left" Height="80" Margin="730,187,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="307"/> 

    </Grid> 
</Page> 

回答

0

如果你正在嘗試做的是有一個按鈕,刷新你的文字,你應該看看MVVM模式,並有綁定更新您的UI。

要做到這一點,你必須創建你的對象,在這種情況下,讓我們說一個人。

public class Person 
{ 
    public string Name { get; set; } 
} 

其次,你會希望有一個人在一個viewmodel內,你會用你的按鈕更新。視圖模型將從BindableBase派生,BindableBase是Windows應用商店應用程序的一部分,如果您使用諸如基本頁面之類的東西。視圖模型看起來是這樣的:

public class MainPageViewModel : BindableBase 
{ 
    public MainPageViewModel() 
    { 

    } 

    private Person person; 
    public Person Person 
    { 
     get { return person; } 
     set { SetProperty(ref person, value); } 
    } 

    public void LoadData() 
    { 
     Person = new Person() { Name = "Simple name" }; 
    } 

    public void UpdatePerson() 
    { 
     Person.Name = "Updated Name"; 
     OnPropertyChanged("Person"); 
    } 
} 

,倘若你沒有這個bindableBase,它看起來像這樣:

[Windows.Foundation.Metadata.WebHostHidden] 
public abstract class BindableBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null) 
    { 
     if (object.Equals(storage, value)) return false; 

     storage = value; 
     this.OnPropertyChanged(propertyName); 
     return true; 
    } 

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     var eventHandler = this.PropertyChanged; 
     if (eventHandler != null) 
     { 
      eventHandler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

在你的你的MainPage創建視圖模型,並設置在DataContext你的頁面。你也想處理您的視圖模型內的對象,所以單擊將修改Person對象的按鈕,當您將創建一個更新方法:

public sealed partial class MainPage : Page 
{ 
    private readonly MainPageViewModel viewModel; 

    public MainPage() 
    { 
     this.InitializeComponent(); 
     viewModel = new MainPageViewModel(); 
     viewModel.LoadData(); 
     this.DataContext = viewModel; 
    } 

    private void Button_Tapped(object sender, TappedRoutedEventArgs e) 
    { 
     viewModel.UpdatePerson(); 
    } 
} 

最後你在UI文本框在人的指向命名視圖模型內部屬性:

<TextBox 
     x:Name="textB" 
     Text="{Binding Person.Name}" 
     HorizontalAlignment="Left" 
     Height="80" 
     Margin="730,187,0,0" 
     TextWrapping="Wrap" 
     VerticalAlignment="Top" 
     Width="307" /> 

我希望這滿足你的你如何能有一個按鈕更新UI的問題。

+0

謝謝。但文本框的值仍然是空的,而不是顯示hello umar。 – Umar 2013-04-24 14:40:48

+0

如果你這樣做,在runmethod()'async'(或者'runmethod()')中沒有任何意義。 – svick 2013-04-24 16:36:18

+0

仍然無法正常工作。只要考慮我的代碼即可顯示。這是我的應用程序的完整代碼。不要去更多的細節。簡單的是,當按鈕點擊我的UI更新。我需要這個功能。 – Umar 2013-04-24 17:25:38