问题
Okay, before you say anything I know multiple posts of this exist and I am searching for an answer for hours. I am a new user in Stack Overflow and fairly new to C# and UWP so please feel free to correct me.
I am making an UWP store application for Windows 10.
I using an API and I am connecting to a server.
All of that happens in a separate page of my MainPage.xaml
which is loaded in a Frame
!
What I want to do is to show the string connectionStatus
to the MainPage.xaml
(through a <TextBlock/>
) while it is changing in my LoginPage.xaml.cs
(which is inside the MainPage.xaml
frame). I am using INotifyPropertyChanged
.
If any of those doesn't make sense please write a comment and I will try to answer.
So I have in my MainPage.xaml
this code for the binding and the <Page.DataContext>
for the NotifyChanges
class (which is in my 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"/>
And here is my NotifyChanges
class which is in my LoginPage.xaml.cs
which is loaded inside a Frame
in my 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");
}
}
}
}
Last but not least, here is my code that changes the connectionStatus
in two different places, while connecting and after connection.
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!";
}
}
Notes:
The binding seems to work because as you can see I have set the default value to "Disconected" and it is set, but after that it never changes again.
I have debugged the program and the app goes into the NotifyChanges definition, but it never executes the notify event because PropertyChanged
never changes from Null.
Am I missing something ? Is my error elsewhere ? Thanks in advance!
回答1:
Yes, you are missing something - this pice of code:
<Page.DataContext>
<local:NotifyChanges/>
</Page.DataContext>
and this one:
NotifyChanges notifyChanges = new NotifyChanges();
create two different references. When you work with binding you want to work on the same one reference of the class. If your Run()
method is in the same page this may work:
public async Task Run()
{
NotifyChanges notifyChanges = this.DataContext as NotifyChanges;
// rest of the code
来源:https://stackoverflow.com/questions/42354995/propertychanged-is-null-uwp