首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >属性更改后Template10 UI不刷新

属性更改后Template10 UI不刷新
EN

Stack Overflow用户
提问于 2016-11-22 21:49:15
回答 1查看 218关注 0票数 3

我正在使用模板10开发一个UWP应用程序,而且在ViewModel中更改属性后,我无法更新UI。我试图在Model中实现Bindable基础,但仍然无法工作。

XAML:

代码语言:javascript
复制
<Page.DataContext>
    <vm:RoomPageViewModel x:Name="ViewModel" />
</Page.DataContext>

<Grid x:Name="RoomProperties"
    RelativePanel.Below="pageHeader"
    RelativePanel.AlignLeftWithPanel="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <Image Grid.Column="0" Width="220" Height="220" Stretch="Fill" Source="{x:Bind ViewModel.Room.Image}"></Image>
    <TextBlock Grid.Column="1" FontSize="16" Text="{x:Bind ViewModel.Room.Name}"></TextBlock>
    <TextBlock Grid.Column="0" Grid.Row="1" FontSize="16" Text="Room Type: "></TextBlock>
    <TextBlock Grid.Column="1" Grid.Row="1" FontSize="16" Text="{x:Bind ViewModel.Room.Type}"></TextBlock>
    <TextBlock Grid.Column="0" Grid.Row="2" FontSize="16" Text="Room Number: "></TextBlock>
    <TextBlock Grid.Column="1" Grid.Row="2" FontSize="16" Text="{x:Bind ViewModel.Room.Number}"></TextBlock>
</Grid>
<ListView x:Name="SensorListView"
          ItemsSource="{x:Bind ViewModel.Room.Sensors}"
          IsEnabled="False"
          RelativePanel.Below="RoomProperties"
          RelativePanel.AlignLeftWithPanel="True">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="data:Sensor">
            <StackPanel HorizontalAlignment="Left">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" FontSize="16" Text="{x:Bind Name}"></TextBlock>
                    <TextBlock Grid.Column="1" FontSize="16" Text="{x:Bind SensorValues[0].Value, Mode=TwoWay}"></TextBlock>
                    <TextBlock Grid.Column="2" FontSize="16" Text="{x:Bind Units}"></TextBlock>
                </Grid>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

ViewModel:

代码语言:javascript
复制
public class RoomPageViewModel : ViewModelBase
{
    Template10.Services.SerializationService.ISerializationService _SerializationService;
    private FileIOHelper.FileIOHelper fileIOHelper = new FileIOHelper.FileIOHelper();
    private AppServiceConnection serialCommandService;

    // 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.

    public RoomPageViewModel()
    {
        if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
        {
            Room = Room.CreateNewRoom();
        }
    }

    private Room room = Room.CreateNewRoom();
    public Room Room
    {
        get
        {
            return this.room;
        }

        set
        {
            Set(ref room, value);
        }
    }

    public void UpdateRoom()
    {
        foreach (var sensor in Room.Sensors)
        {
            var sensorValue = new SensorValue();
            sensorValue.Sensor = "R" + Room.Number + "D" + sensor.DeviceNumber + "S" + sensor.Type;
            ObservableCollection<SensorValue> newList = fileIOHelper.ReadFromFile(sensorValue).ToObservableCollection();
            SensorValue newSensorValue = newList.Last();
            sensor.SensorValues = new ObservableCollection<SensorValue> { newSensorValue };
        }
        foreach (var actuator in Room.Actuators)
        {
            var actuatorValue = ActuatorValue.CreateNewActuatorValue();
            actuatorValue.Actuator = "R" + Room.Number + "D" + actuator.DeviceNumber + "A" + actuator.Type;
            ObservableCollection<ActuatorValue> newList = fileIOHelper.ReadFromFile(actuatorValue).ToObservableCollection();
            ActuatorValue newActuatorValue = newList.Last();
            actuator.ActuatorValues = new ObservableCollection<ActuatorValue> { newActuatorValue };
        }
    }

    public async void RefreshButton_Click(object sender, object parameter)
    {
        Random rnd = new Random();
        Room = Room.CreateNewRoom(rnd.Next(1, 9));
        //UpdateRoom();
        await Task.CompletedTask;
    }

型号:

代码语言:javascript
复制
public class Room : BindableBase
{
    private string name;
    private string image;
    private string type;
    private int number; 

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            Set(ref name, value);
        }
    }

    public string Image
    {
        get
        {
            return image;
        }
        set
        {
            Set(ref image, value);
        }
    }

    public string Type
    {
        get
        {
            return type;
        }
        set
        {
            Set(ref type, value);
        }
    }

    public int Number
    {
        get
        {
            return number;
        }
        set
        {
            Set(ref number, value);
        }
    }

    private ObservableCollection<Sensor> sensors;

    private ObservableCollection<Actuator> actuators;

    public ObservableCollection<Sensor> Sensors
    {
        get
        {
            return sensors;
        }
        set
        {
            Set(ref sensors, value);
        }
    }

    public ObservableCollection<Actuator> Actuators
    {
        get
        {
            return actuators;
        }
        set
        {
            Set(ref actuators, value);
        }
    }

    public Room() {
        Random rnd = new Random();
        Name = "DefaultName";
        Image = "DefaultImage";
        Type = "DefaultType";
        Number = rnd.Next(1,9);
        Sensors = new ObservableCollection<Sensor>();
        Actuators = new ObservableCollection<Actuator>();
    }

    public Room(int inputNumber)
    {
        Name = "DefaultName";
        Image = "DefaultImage";
        Type = "DefaultType";
        Number = inputNumber;
        Sensors = new ObservableCollection<Sensor>();
        Actuators = new ObservableCollection<Actuator>();
    }

    public static Room CreateNewRoom(int inputNumber)
    {
        return new Room(inputNumber);
    }
}

我使用这个指南作为实现(https://github.com/Windows-XAML/Template10/wiki/MVVM)的文档。知道为什么UI没有更新吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-23 10:31:10

大多数人(包括我自己)在习惯“旧”Binding语法时经常犯的错误是,x:Bind默认使用OneTime绑定而不是OneWay绑定。

mode :指定绑定模式,作为以下字符串之一:"OneTime“、"OneWay”或"TwoWay“。默认情况是"OneTime“。请注意,这与默认的{Binding}不同,在大多数情况下,{Binding}是"OneWay“。

来源:MSDN

要使绑定更新工作,您需要的是:

  • 使用INotifyPropertyChanged,这将由BindableBase处理。
  • 设置正确的模式。 Text="{x:Bind ViewModel.Room.Number, Mode=OneWay}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40752271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档