C# Metro用户界面未更新
我刚开始开发metro应用程序,我面临的问题是dispatcher没有更新UI。我的代码如下,请让我知道是什么问题C# Metro用户界面未更新,c#,xaml,microsoft-metro,c#-5.0,C#,Xaml,Microsoft Metro,C# 5.0,我刚开始开发metro应用程序,我面临的问题是dispatcher没有更新UI。我的代码如下,请让我知道是什么问题 public class Test : DependencyObject { public static readonly DependencyProperty CurrentItemProperty = DependencyProperty.Register("NameOfPerson", typeof(string), typeof(Test), nul
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>
如果您正在尝试让按钮刷新文本,那么您应该查看MVVM模式,并让绑定更新您的UI 要做到这一点,你必须创建你的对象,在这种情况下,让我们假设一个人
public class Person
{
public string Name { get; set; }
}
其次,您希望在viewmodel中有一个人,您将使用按钮更新该人。viewmodel将派生自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));
}
}
}
最后,在UI中的文本框指向Viewmodel中的Person's 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呢??
<TextBox
x:Name="textB"
Text="{Binding Person.Name}"
HorizontalAlignment="Left"
Height="80"
Margin="730,187,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="307" />