gpt4 book ai didi

c# - 导航期间多次调用 ValueConverter

转载 作者:太空宇宙 更新时间:2023-11-03 13:17:39 24 4
gpt4 key购买 nike

在与 Binding 一起使用的 Converter 中,有些东西我无法理解。 :-(

我用 Mvvm-Light 创建了一个简单的例子......

这里是完整的解决方案:https://testbindingwithconverter.codeplex.com/SourceControl/latest

这里是一张图片:http://i.stack.imgur.com/wUf89.png

和摘要源代码下方:

CarsView

<Rectangle Grid.Row="0" 
Opacity="{Binding SelectedCar, Converter={StaticResource IntToOpacityConverter}}"
Fill="#FFD1E22A" />
<ListBox Grid.Row="1"
ItemsSource="{Binding Cars}"
SelectedItem="{Binding SelectedCar, Mode=TwoWay}" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Company}" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Year}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

CarsViewModel

public class CarsViewModel : ViewModelBase
{
private ObservableCollection<Car> cars;
private Car selectedCar;

public CarsViewModel()
{
if (IsInDesignMode)
{
}
else
{
// INIT
this.cars = new ObservableCollection<Car>();
this.selectedCar = null;

// FAKE DATA
for (int i = 1; i <= 10; i++)
{
Car newCar = new Car { Id = i, Company = "Company_" + i, Name = "Name_" + i, Year = "200" + i };
Cars.Add(newCar);
}
}
}

public ObservableCollection<Car> Cars
{
get
{
return cars;
}

set
{
if (cars != value)
{
cars = value;
RaisePropertyChanged(() => Cars);
}
}
}

public Car SelectedCar
{
get
{
return selectedCar;
}

set
{
if (value != selectedCar)
{
selectedCar = value;
RaisePropertyChanged(() => SelectedCar);
}
}
}
}

转换器

public class IntToOpacityConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
double opacity = 1;
Car c = value as Car;

if (c != null)
{
if (c.Id == 5)
{
opacity = 0.3;
}
}

System.Diagnostics.Debug.WriteLine("[IntToOpacityConverter] \t" + "Rectangle Opacity: " + opacity);
return opacity;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
// One-Way conversion!
return null;
}

在 CarsView 中,当我点击 ListBox 的第 5 项时,Rectangle 的属性 Opacity 设置为值 0.3,否则设置为值 1。

现在,当我返回到 HomeView 然后再次返回到 CarsView 时,如果我在项目之间点击转换器被调用 2 次 !再一次,如果我返回到 HomeView 并返回到 CarsView,转换器被调用 3 次!等等……

为什么?

最佳答案

嘿文森佐佩特罗尼奥,

我不会在每次导航时创建一个新的 CarsViewModel(),因为它不会将您的颜色保存在 Rectangle 上,相反,您希望在导航离开时取消绑定(bind) DataContent。

CarsView.xaml/CarsView.xaml.cs 上创建一个卸载事件:

<phone:PhoneApplicationPage  Unloaded="PhoneApplicationPage_Unloaded">
private void PhoneApplicationPage_Unloaded(object sender, RoutedEventArgs e)
{
this.DataContext = null;
}

然后您需要检查 Car Set 函数是否为空值,这样它就不会将您的选择更改为空值。

public Car SelectedCar
{
get
{
System.Diagnostics.Debug.WriteLine("[CARSVIEWMMODEL] \t" + "GET SelectedCar");
return selectedCar;
}

set
{
if (value != selectedCar && value != null)
{
selectedCar = value;
System.Diagnostics.Debug.WriteLine("[CARSVIEWMMODEL] \t" + "SET SelectedCar");
RaisePropertyChanged(() => SelectedCar);
}
}
}


如果您应用更改,您会看到它会按照您想要的方式运行,而且当您导航回“汽车”页面时它会保存该矩形的颜色。

关于c# - 导航期间多次调用 ValueConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25490577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com