2016-11-22 16 views
3

テンプレート10を使用してUWPアプリケーションで作業していますが、ViewModelでプロパティが変更された後にUIを更新することはできません。モデルでBindableベースを実装しようとしましたが、まだ動作しません。Template10プロパティが変更された後にUIがリフレッシュされない

XAML:

<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:

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

モデル:

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が更新されない理由についての考え方はありますか?ありがとう。 (私を含め)ほとんどの人が、多くの場合、「古い」Binding構文に使用されたときに作る

+0

、私はこの問題は、バインディングは、これがデフォルト値ではありませんよう、一方向である必要があることかもしれませんお読みください。私はしばらくしてからやってみます。 – Carlos

答えて

2

間違いはx:Bindは、デフォルトの代わりに、OneWay結合として結合OneTimeを持っているということです。

モードは:「ワンタイム」、「一方向」、または「双方向」:これらの文字列の一つとして、結合モードを指定します。デフォルトは "OneTime"です。これは、ほとんどの場合「OneWay」である{Binding}のデフォルトとは異なります。

出典:MSDN

あなたが仕事にあなたの結合更新のために必要なものは次のとおりです。

INotifyPropertyChangedを使用して
  • 、これはBindableBaseによって処理されます。
  • 正しいモードを設定します。さらなる調査の後

    Text="{x:Bind ViewModel.Room.Number, Mode=OneWay}

+0

ありがとうございます。現在、ObservableCollectionsの要素の1つの中で何かを変更すると、メインプロパティの更新が行われます。UIは更新されません。私が持っている構造は:Room(Properties + Actuator ObservableColletion)=> Actuator(Properties + ActuatorStatus ObservableColletion)です。 BindableBaseをActuatorとActuatorStatusの両方のモデルで実装していますが、UIは更新されません。バインディングはすべてOneWayになりました。 – Carlos

+0

何らかの理由で、ListView内で "{x:Bind xxx、Mode = OneWay}"を使用してUIを更新することはできませんが、 "{Binding xxx、Mode = OneWay}"は完全に機能します。あなたの助けをもう一度ありがとう。 – Carlos

+0

@Carlosあなたはx:バインドの振る舞いを再現できますか?あなたが確実に私にreproプロジェクトへのリンクを送ることができれば、私はそれについてチームに伝えます。できるだけ私がMicrosoft.comの@ Jerry.Nixonにメールしてください。 –

関連する問題