C# 是否可以在UWP中动态更改/删除SwipeControl的SwipeItems?

C# 是否可以在UWP中动态更改/删除SwipeControl的SwipeItems?,c#,uwp,uwp-xaml,C#,Uwp,Uwp Xaml,我正在尝试使用Microsofts newSwipeControl来允许用户显示项目可用的操作。我正在努力的是删除SwipeItems当操作不再可用时,我知道可以设置SwipeControl上的LeftItems属性并将其置零,但我需要它比这更具动态性 关于设置的一些注意事项,我使用Caliburn Micro作为MVVM框架,并实现INotifyPropertyChanged。我没有使用x:Bind 尝试1-基于开关控制的控制 我尝试基于SwipeControl创建一个新控件。我为LeftIt

我正在尝试使用Microsofts new
SwipeControl
来允许用户显示项目可用的操作。我正在努力的是删除
SwipeItems
当操作不再可用时,我知道可以设置
SwipeControl
上的
LeftItems
属性并将其置零,但我需要它比这更具动态性

关于设置的一些注意事项,我使用Caliburn Micro作为MVVM框架,并实现INotifyPropertyChanged。我没有使用x:Bind

尝试1-基于开关控制的控制 我尝试基于
SwipeControl
创建一个新控件。我为
LeftItemsEnabled
InternalLeftItems
添加了Dependencyprops。InternalLeftItems用于存储每次删除和重置之间的项目列表。要删除和重置
LeftItems
属性,我通过更改布尔值使用
LeftItemsEnabled
属性。我将
LeftItems
设置为我的
InternalLeftItems
值或
null
。代码见下文:

依赖属性和PropertyChanged实现

public static readonly DependencyProperty EnableLeftItemsProperty =
  DependencyProperty.Register("EnableLeftItems", typeof(bool), typeof(UpdatedSwipeControl), new PropertyMetadata(false, LeftEnabledPropertyChanged));

public static readonly DependencyProperty InternalLeftItemsProperty =
  DependencyProperty.Register("InternalLeftItems", typeof(SwipeItems), typeof(UpdatedSwipeControl), new PropertyMetadata(new SwipeItems()));

public SwipeItems InternalLeftItems {
  get { return (SwipeItems)GetValue(InternalLeftItemsProperty); }
  set { SetValue(InternalLeftItemsProperty, value); }
}

private static void LeftEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
  var control = d as UpdatedSwipeControl;
  if (control == null) return;

  control.EnableLeftItems = (bool)e.NewValue;
  if ((bool)e.NewValue)
      control.RightItems = control.InternalRightItems;
    else 
      control.RightItems = null;

    UpdateLayout();
}
视图中的用法

<Page.Resources>
  <SymbolIconSource x:Key="ButtonIconSource"
                    Symbol="Cancel" />
  <SwipeItems x:Key="Items">
     <SwipeItem Text="SWIPE ITEM"
                IconSource="{StaticResource ButtonIconSource}"
                Background="Red" />
  </SwipeItems>
</Page.Resources>

<controls:UpdatedSwipeControl x:Name="swipeControl"
                              EnableLeftItems={Binding ItemsEnabled}
                              InternalLeftItems="{StaticResource Items}">
  <TextBlock Text="SWIPE" />
</SwipeControl>
此尝试的问题是,在初始绑定上从默认值
False
更改为
True
时,仅触发一次
LeftEnabledPropertyChanged
。它不会在任何后续的NotifyPropertyChanged上再次激发。我不确定我的DependencyOps或PropertyChangedEvent设置中是否遗漏了一些内容

尝试2-LeftItems道具上的转换器: 我的第二次尝试是在
布尔值的绑定上使用
转换器。我创建了一个简单的转换器来返回我的资源中的SwipeItems或
null
。代码见下文:

转换器代码

public class ItemsConverter : IValueConverter {

  public SwipeItems items { get; set; }

  public object Convert(object value, Type targetType, object parameter, string language)
  {
    return (bool) value ? items : null;
  }

  public object ConvertBack(object value, Type targetType, object parameter, string language)
  {
    throw new NotImplementedException();
  }
}
实施展望

<Page.Resources>
  <sampleApp:ItemsConverter x:Key="converter" items="{StaticResource Items}"/>
  <SwipeItems x:Key="Items">
     <SwipeItem Text="SWIPE ITEM"
                IconSource="{StaticResource ButtonIconSource}"
                Background="Red" />
  </SwipeItems>
</Page>

<SwipeControl x:Name="swipeControl"
              LeftItems="{Binding ItemsEnabled, Converter={StaticResource converter}}">
  <TextBlock Text="SWIPE" />
</SwipeControl>

ViewModel上ItemsEnabled属性的相同实现。 此尝试的问题是转换器从未命中,并且没有绑定错误


总之,我知道可以通过代码隐藏设置/重置SwipeItems属性,该属性在非常简单的场景中正常工作。但是任何更有活力的东西我都不能去工作。是否有其他人遇到过此问题或对此有解决方法?

好吧,这很尴尬,在离开几天并重新编译后,尝试2使用转换器按预期工作。我不知道为什么它以前不起作用。不过,我不知道该怎么处理这个问题,我是否应该把它留给其他人,以防他们尝试相同的事情?SwipeControl仅支持触摸设备,您使用哪种设备进行了测试?我已经使用Surface Pro 3进行了测试。在我今天早上重新编译后,它正在使用转换器方式工作。我想知道我是应该把这个问题留着让别人看,还是干脆把它删除。你的代码看起来是正确的,可能是编译问题导致的,你可以删除这个案例。
<Page.Resources>
  <sampleApp:ItemsConverter x:Key="converter" items="{StaticResource Items}"/>
  <SwipeItems x:Key="Items">
     <SwipeItem Text="SWIPE ITEM"
                IconSource="{StaticResource ButtonIconSource}"
                Background="Red" />
  </SwipeItems>
</Page>

<SwipeControl x:Name="swipeControl"
              LeftItems="{Binding ItemsEnabled, Converter={StaticResource converter}}">
  <TextBlock Text="SWIPE" />
</SwipeControl>