【问题标题】:UWP: Go back a page with databound itemsUWP:返回包含数据绑定项目的页面
【发布时间】:2016-06-17 10:58:36
【问题描述】:

我有一个带有参数“名称”、“位置”(或“房间”,如果需要)和“位置 2”(或“楼层”)的数据集,如下所示:

Location2      Location       Name
1st floor      Living room    Lights
1st floor      Living room    Temperature
1st floor      Living room    Thermostat
1st floor      Kitchen        Lights     
2nd floor      Bedroom        Lights

等等。

我有两个组合框分别绑定到 Location2 和 Location。 “位置”的项目取决于选择了“位置 2”。当您首先选择一个楼层(“位置”)然后选择一个房间(“位置”)时,您将获得所选房间中的所有设备。

这很好用,像这样:

这是绑定的代码:

<ComboBox x:Name="Location2Combobox" Margin="5,10,5,5" Width="120" ItemsSource="{x:Bind ViewModel.Location2, Mode=OneWay}" SelectedValue="{x:Bind ViewModel.Location2Filter, Mode=TwoWay}"  />
<ComboBox x:Name="Location1Combobox" Margin="5,10,5,5" Width="120" ItemsSource="{x:Bind ViewModel.Location1, Mode=OneWay}" SelectedValue="{x:Bind ViewModel.Location1Filter, Mode=TwoWay}" />

组合框在 OnNavigatedToAsync() 上是数据绑定的

public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary<string, object> suspensionState)
{
....
    if (Location2 == null) Location2 = (from d in App.HS.DeviceList
                                        orderby d.location2 ascending
                                        select d.location2).Distinct().ToList();

    if (Location1 == null) Location1 = (from d in App.HS.DeviceList
                                        where d.location2 == Location2Filter
                                        orderby d.location ascending
                                            select d.location).Distinct().ToList();
}

... 但是 Location1Combobox 的数据源也会在 Location2Filter 更新时更新。 (当用户选择楼层时,房间的组合框会更新)。

    string _Location2Filter = "";
    public string Location2Filter
    {
        get { return _Location2Filter; }
        set
        {
            Set(ref _Location2Filter, value);
            UpdateLocation1();
        }
    }

    public void UpdateLocation1()
    {
        Debug.WriteLine("MainPageViewModel UpdateLocation1(), navigateBack: " + navigateBack.ToString());

        if (Location2Filter != "")
        {
            Location1 = (from d in App.HS.DeviceList
                         where d.location2 == Location2Filter
                         orderby d.location ascending
                         select d.location).Distinct().ToList();
        }
    }

这一切都很好! :)

但是,当我单击一个 ListItem 时,我会导航到一个新页面。 但是当我返回时,房间组合框被重置了! 由于 NavigationCacheMode,地板组合框选择了最后选择的值。但不是房间组合框。

我无法全神贯注于我需要做的事情。

【问题讨论】:

    标签: c# mvvm uwp


    【解决方案1】:

    我做了一个演示并重现了您的问题。在调试过程中我发现问题在于以下代码:

    public string Location2Filter
    {
        get { return _Location2Filter; }
        set
        {
            Set(ref _Location2Filter, value);
            UpdateLocation1();
        }
    }
    

    每次导航到新页面时,都会调用 updateLocation(),这会导致 location1 组合框为空。

    要解决此问题,您可以编辑如下代码:

    public string Location2Filter
    {
        get { return _Location2Filter; }
        set
        {
            if (_Location2Filter != value) {
                Set(ref _Location2Filter, value);
                UpdateLocation1();
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      相关资源
      最近更新 更多