【问题标题】:ReactiveUI Binding does not work in Release build in a UWP appReactiveUI 绑定在 UWP 应用程序的发布版本中不起作用
【发布时间】:2017-04-08 05:37:41
【问题描述】:

我在 ReactiveUI 和绑定中遇到了一个奇怪的问题,其中绑定在 Debug 构建中可以正常工作,但在 Release 构建中却不行。

这里我有一个显示问题的示例应用程序的代码。在 Debug 构建中,当我在文本框中键入内容时,视图模型中的 InputText 属性会相应更新,当我点击按钮时,它会在消息对话框中显示更新的输入文本。但是 Release 版本中的相同代码不起作用,因为 InputText 始终为空。

有人知道这里发生了什么吗?

<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();   
        });
    }
}

【问题讨论】:

  • @Sunteen Wu,你真的尝试过上面的方法,得出的结论是无法重现吗?上面的代码实际上是我在示例项目中编写的所有内容,并且没有遗漏任何一行。
  • 即使在最新的 UWP 和 .NET Native 工具上,我也能重现您的问题。它类似于github.com/reactiveui/ReactiveUI/issues/1330,我也有兴趣被追赶。您可以在那里加入讨论,或者发送电子邮件至 dotnetnative@microsoft.com。我可能需要一两天的时间来挖掘。
  • 对不起,我的错。测试时错过了ReactiveUI 第三方包。已删除。

标签: xaml data-binding uwp reactiveui .net-native


【解决方案1】:

这并不是对“这里发生了什么”问题的真正答案,但我是提交了 Matt Whilden 链接到的另一个错误的人,我暂时通过参考文本来解决它按钮事件并从那里调用命令,而不是将命令直接绑定到按钮,有点像这样:

d(Observable.FromEventPattern<RoutedEventHandler, object, RoutedEventArgs>(h => MyButton.Click += h, h => MyButton.Click -= h).Subscribe(x=>
{
  ViewModel.InputText = MyTextBox.Text;
  ViewModel.MyButtonCommand.Execute(null);
}));

不优雅,但它对我有用,因为我真的不需要更新属性更改 - 只需单击按钮即可。在问题解决之前,也许这对您也有用。

【讨论】:

  • Execute 中的 null 是虚假的,但这确实解决了报告的问题。我会继续挖掘。我的调查笔记将在 GitHub 线程中。
【解决方案2】:

正如 Matt Whilden 在 this thread 中提到的那样,使用运行时指令方法可以解决问题,因此我将其标记为正确答案。非常感谢 Matt Whilden。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 2021-04-01
    • 2022-07-27
    • 1970-01-01
    • 2017-02-15
    • 2018-03-15
    相关资源
    最近更新 更多