2017-02-20 33 views
1

好吧,在你说任何事之前,我知道有多个这样的帖子,我正在寻找答案几个小时。我是堆栈溢出的新用户,对于C#和UWP相当新,所以请随时纠正我。PropertyChanged是空的UWP

我正在为Windows 10制作UWP商店应用程序。 我正在使用API​​并且正在连接到服务器。 所有这一切都发生在我的MainPage.xaml的单独页面中,该页面已加载到Frame

我想要做的是显示string connectionStatusMainPage.xaml(通过<TextBlock/>),而它在我的LoginPage.xaml.cs变化(这是MainPage.xaml框内)。我正在使用INotifyPropertyChanged。 如果其中任何一个都没有意义,请发表评论,我会尽力回答。

所以我在我的MainPage.xaml这个代码的绑定和<Page.DataContext>NotifyChanges类(这是我的LoginPage.xaml.cs)。

<Page.DataContext> 
    <local:NotifyChanges/> 
</Page.DataContext> 

<TextBlock Text="{Binding ConnectionStatus, Mode=OneWay}" Margin="0,0,10,0" Foreground="LimeGreen" FontWeight="Bold" VerticalAlignment="Center" HorizontalAlignment="Right" FontSize="18"/> 

这里是我的NotifyChanges类,这是在我的LoginPage.xaml.cs这是在加载Frame里面我MainPage.xaml

public class NotifyChanges : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    private string connectionStatus = "Disconnected"; 

    public string ConnectionStatus 
    { 
     get 
     { 
      return connectionStatus; 
     } 

     set 
     { 
      if (value != connectionStatus) 
      { 
       connectionStatus = value; 
       NotifyPropertyChanged("ConnectionStatus"); 
      } 
     } 
    } 
} 

最后但并非最不重要的,这里是我的代码改变在两个connectionStatus不同的地方,而连接和连接后。

public async Task Run() 
{ 
    NotifyChanges notifyChanges = new NotifyChanges(); 

    try 
    { 
     userToken = TokenTextBox.Text; 

     await client.LoginAsync(TokenType.User, userToken); 

     var connect = client.ConnectAsync(); 

     notifyChanges.ConnectionStatus = client.ConnectionState.ToString(); 

     await connect; 

     notifyChanges.ConnectionStatus = client.ConnectionState.ToString(); 

     Frame.Navigate(typeof(ChatPage), userToken); 

     // Block this task until the program is exited. 
     await Task.Delay(-1); 
    } 
    catch 
    { 
     ConnectionErrorTextBlock.Text = "Something went wrong :/ You may want to check the token again!"; 
    } 
} 

注: 结合似乎工作,因为你可以看到我已设置的默认值“Disconected”,它被设置,但之后,它将不再改变。 我调试了程序,应用程序进入NotifyChanges定义,但它从不执行通知事件,因为PropertyChanged从不会从Null更改。 我错过了什么吗?我的错误在别处吗?提前致谢!

回答

1

是的,你缺少的东西 - 这PICE代码:

<Page.DataContext> 
    <local:NotifyChanges/> 
</Page.DataContext> 

这一个:

NotifyChanges notifyChanges = new NotifyChanges(); 

创建两个不同的引用。当你使用绑定工作时,你想要在类的同一个引用上工作。如果您的Run()方法是在同一页面,这可能工作:

public async Task Run() 
{ 
    NotifyChanges notifyChanges = this.DataContext as NotifyChanges; 
    // rest of the code 
+0

它的工作原理。谢谢你,兄弟。几个小时一直在尝试这个。你能解释为什么会发生这种情况吗? this.DataContext指向Login.xaml.cs的同一页面。该绑定发生在页面MainPage中。这是如何运作的 ?谢谢! – KonKarapas

+0

@KonKarapas绑定只是指一个来自DataContext的类,在你的情况下,它是在xaml中创建的。在你的解决方案中,在Run方法中,你已经创建了全新的引用,然后你一直在修改,但它不是来自数据上下文的引用。用户界面绑定到在xaml中创建的用户界面。 – Romasz

+0

@KonKarapas请注意,您也可以在页面的构造函数中分配* NotifyChanges *到一个合适的属性,然后设置'this.DataContext = propertyNotifyChanges;' - 然后您有一个代码中的属性,您可以轻松修改而无需投射/创建在xaml中设置DataContext)。你也可以在不设置* DataContext *的情况下使用'x:Bind',但这是另外一个故事 - 有很多关于它的博客/文章。 – Romasz