Reputation: 599
I'm hitting a weird issue with ReactiveUI and binding where the binding works fine in Debug build but not in Release build.
Here I have the code for a sample app that shows the issue. In Debug builds, as I type something in the textbox the InputText property in the view model gets updated accordingly and when I tap the button it shows the updated input text back to me in a message dialog. But the same code in Release build does not work, as InputText always remains empty.
Anyone knows what's going on here?
<Page x:Class="RxBind.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<TextBox x:Name="MyTextBox" Margin="10"/>
<Button x:Name="MyButton" Content="Show Dialog" Margin="10"/>
</StackPanel>
</Page>
public sealed partial class MainPage : IViewFor<MainPageViewModel>
{
public MainPage()
{
InitializeComponent();
ViewModel = new MainPageViewModel();
this.WhenActivated(d =>
{
d(this.BindCommand(ViewModel, vm => vm.MyButtonCommand, v => v.MyButton));
d(this.Bind(ViewModel, vm => vm.InputText, x => x.MyTextBox.Text));
});
}
#region IViewFor impl
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (MainPageViewModel)value; }
}
public MainPageViewModel ViewModel { get; set; }
#endregion //IViewFor impl
}
public class MainPageViewModel : ReactiveObject
{
private string _inputText = string.Empty;
public string InputText
{
get { return _inputText; }
set { this.RaiseAndSetIfChanged(ref _inputText, value); }
}
public ReactiveCommand<Unit, Unit> MyButtonCommand { get; }
public MainPageViewModel()
{
MyButtonCommand = ReactiveCommand.CreateFromTask(async () =>
{
await new MessageDialog($"InputText={InputText}").ShowAsync();
});
}
}
Upvotes: 1
Views: 612
Reputation: 599
As Matt Whilden mentioned in this thread, using runtime directive approach solves the problem so I'm marking this as the right answer. Many thanks Matt Whilden.
Upvotes: 0
Reputation: 3653
This isn't really an answer to your question of "what's going on here," but I'm the guy who filed that other bug Matt Whilden linked to and I worked around it for the time being by referencing the text on the button event and calling the command from there too instead of binding the command directly to the button, sort of like this:
d(Observable.FromEventPattern<RoutedEventHandler, object, RoutedEventArgs>(h => MyButton.Click += h, h => MyButton.Click -= h).Subscribe(x=>
{
ViewModel.InputText = MyTextBox.Text;
ViewModel.MyButtonCommand.Execute(null);
}));
Not elegant but it works for me because I don't really need to update on property changed - just on button click. Maybe this will work for you as well before the issue is solved.
Upvotes: 1