I have two radio buttons which are using two-way binding to two boolean properties, X
and Y
of my view model. Since radio buttons are mutually exclusive, setting X
automatically clears Y
and vice versa. Now, if I navigate to a different page, then come back, navigate to it again and come back to the page with radio buttons, the buttons stop working.
Steps to reproduce the problem:
- Create a new C# Universal Windows Blank App.
- Set min and target version to 1809 (the problem persists for 1803 and Fall Creators Update)
In App.xaml.cs, add the following code before line
sealed partial class App : Application
(Do includeSystem.ComponentModel
andSystem.Runtime.CompilerServices
namespaces)public class ViewModel : INotifyPropertyChanged { private bool _X; public bool X { get => _X; set { if (value != _X) { _X = value; OnPropertyChanged(); } } } private bool _Y; public bool Y { get => _Y; set { if (value != _Y) { _Y = value; OnPropertyChanged(); } } } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
and in class
App
, addpublic static ViewModel VM = new ViewModel();
In MainPage.xaml, replace page content with
<StackPanel> <RadioButton IsChecked="{x:Bind VM.X, Mode=TwoWay}" Content="X"/> <RadioButton IsChecked="{x:Bind VM.Y, Mode=TwoWay}" Content="Y"/> <Button Content="Navigate" Click="Button_Click"/> </StackPanel>
and in MainPage.xaml.cs, replace
MainPage
class withpublic sealed partial class MainPage : Page { public ViewModel VM => App.VM; public MainPage() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { Frame.Navigate(typeof(BlankPage1)); } }
Add a blank page to your project and in its .xaml file, replace its page content with
<Grid> <Button Click="Button_Click" Content="Back"/> </Grid>
and in its .cs file, add following button handler
private void Button_Click(object sender, RoutedEventArgs e) { Frame.Navigate(typeof(MainPage)); }
Compile and launch the application and once it's running, click on the radio buttons to confirm that they are working.
Click Navigate, then click Back. The radio buttons are working at this point.
Once again, click Navigate and then click Back. The buttons no longer work.
What causes the buttons to stop working? Is this a bug or am I missing something and the behavior is expected? Thanks.