2013-04-24 140 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