【问题标题】:Metro UI not updateMetro UI 未更新
【发布时间】:2013-04-25 15:14:22
【问题描述】:

我刚刚开始开发 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>

【问题讨论】:

    标签: c# xaml microsoft-metro c#-5.0


    【解决方案1】:

    如果您正在尝试使用按钮刷新您的文本,您应该查看 MVVM 模式并让绑定更新您的 UI。

    为此,您必须创建对象,在这种情况下,假设是一个人。

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

    其次,您希望在视图模型中有一个人,您将使用您的按钮对其进行更新。视图模型将派生自 BindableBase,它是 Windows 应用商店应用程序的一部分,如果您要使用基本页面之类的东西。 Viewmodel 如下所示:

    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 上创建 ViewModel 并在您的 Page 上设置 DataContext。此外,您还想在 Viewmodel 中处理您的对象,因此您将在单击将修改您的 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 指向 Viewmodel 中 Person 的 name 属性:

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

    我希望这能解决您关于如何让按钮更新您的 UI 的问题。

    【讨论】:

    • 谢谢。但是文本框的值仍然是空的,而不是显示 hello umar。
    • 如果你这样做了,那么runmethod()async(或者根本没有runmethod())就没有意义了。
    • 还是不行。只需考虑我显示的代码。这是我的应用程序的完整代码。不要详细说明。很简单,当按钮单击我的 UI 更新时。我需要这个功能。
    • 我建议您使用 MVVM 模式,请参阅上面修改后的答案。
    • 是否有必要使用MVVM模式。如果我不使用 mvvm 怎么办??
    猜你喜欢
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    相关资源
    最近更新 更多