C# 来自DataTemplate对象的UWP调用方法
自学成才的程序员,希望对我的代码提出建设性的批评 我有一个ListView,其中包含我要自定义的ListViewItems 我制作的ListViewItem有两个文本块和一个ToggleSwitch。当切换开关打开/关闭时,我希望它从实例化对象调用一个方法,或者从相同的表单调用一个方法,但是以某种方式检索最初加载到DataTemplate中的对象 以下是迄今为止的XAML:C# 来自DataTemplate对象的UWP调用方法,c#,xaml,win-universal-app,C#,Xaml,Win Universal App,自学成才的程序员,希望对我的代码提出建设性的批评 我有一个ListView,其中包含我要自定义的ListViewItems 我制作的ListViewItem有两个文本块和一个ToggleSwitch。当切换开关打开/关闭时,我希望它从实例化对象调用一个方法,或者从相同的表单调用一个方法,但是以某种方式检索最初加载到DataTemplate中的对象 以下是迄今为止的XAML: <ListView x:Name="listViewAddedVideoFolders" Grid.Row=
<ListView x:Name="listViewAddedVideoFolders" Grid.Row="1" DoubleTapped="listViewAddedVideoFolders_DoubleTapped" SelectionChanged="listViewAddedVideoFolders_SelectionChanged" HorizontalContentAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
<TextBlock HorizontalAlignment="Stretch" VerticalAlignment="Center" Text="{Binding Directory}"/>
<Grid HorizontalAlignment="Right">
<StackPanel>
<TextBlock Text="Find Videos: "></TextBlock>
<ToggleSwitch Toggled="listViewVideoFolder_toggled" />
</StackPanel>
</Grid>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListView.ItemContainerStyle>
现在它正在调用listViewVideoFolder\u toggled
在尝试使用Toggled=“{Binding StartCrawling()}”之前
下面是我将listviewitems绑定到的AddVideoFolderModel对象
namespace Movie_Management_Windows_10.Models
{
public class AddVideoFolderModel
{
public static ObservableCollection<AddVideoFolderModel> MyVideoFolderModels = new ObservableCollection<AddVideoFolderModel>();
public int VideosFound { get; set; }
public string Directory { get; set; }
public string DirectoryName { get; set; }
private bool isCrawling = false;
public bool HasBeenCrawled = false;
private void startCrawling()
{
AppShell.Current.NotifyUser("Crawling began", AppShell.NotifyType.StatusMessage);
}
//public override string ToString()
//{
// return Directory + " (" + VideosFound.ToString() + ")";
//}
}
}
namespace Movie\u Management\u Windows\u 10.Models
{
公共类AddVideoFolderModel
{
公共静态ObservableCollection MyVideoFolderModels=新ObservableCollection();
public int VideosFound{get;set;}
公共字符串目录{get;set;}
公共字符串目录名{get;set;}
private bool isCrawling=假;
公共bool HasBeenCrawled=false;
私有无效开始绘制()
{
AppShell.Current.NotifyUser(“爬行开始”,AppShell.NotifyType.StatusMessage);
}
//公共重写字符串ToString()
//{
//返回目录+“(“+VideosFound.ToString()+”);
//}
}
}
要实现这一点,我必须实现什么?首先,您可以将属性添加到模型中,并使用
双向
模式绑定绑定到ToggleSwitch
中的IsOn
属性。在这种情况下,您的模型必须实现INotifyPropertyChanged
private bool _isNeedCrawle;
public bool IsNeedCrawle
{
get
{
return _isNeedCrawle;
}
set
{
if (_isNeedCrawle != value)
{
_isNeedCrawle = value;
if (_isNeedCrawle)
{
startCrawling();
}
NotifyPropretyChanged("IsNeedCrawle");
}
}
}
其次,您可以使用XAML行为SDK。在这种情况下,必须添加对库()的引用,并将方法修饰符从private
更改为public
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"
<ToggleSwitch>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="Toggled">
<core:CallMethodAction MethodName="StartCrawling" TargetObject="{Binding }"/>
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</ToggleSwitch>
xmlns:i=“使用:Microsoft.Xaml.Interactivity”
xmlns:core=“使用:Microsoft.Xaml.Interactions.core”
我确实尝试了InotifyProperty更改,我将再次尝试。谢谢名称空间与这些对象不存在哪一个?第一个还是第二个?第二个有问题。什么问题?您是否添加了对库的引用?