【发布时间】:2020-07-20 10:23:25
【问题描述】:
我正在使用 RelayCommand,它由 Microsoft 在其 ICommand example 中实现。这是我的代码简介。
XAML:
<TextBox x:Name="YoutubeUrlText"
PlaceholderText="Insert link from youtube here... "
FontSize="20" FontStyle="Italic" Width="450" />
<Button x:Name="ConvertButton" FontFamily="Segoe MDL2 Assets" FontSize="20" Content=""
Grid.Column="1" Height="40"
HorizontalAlignment="Left" VerticalAlignment="Center"
Foreground="White" BorderBrush="#FF797979" Background="#FF0096FF"
Command="{x:Bind urlSearchViewModel._GetVideo(YoutubeUrlText.Text)}"/>
XAML 背后的代码:
public sealed partial class UrlSearchPage : Page
{
public UrlSearchViewModel urlSearchViewModel;
public UrlSearchPage()
{
this.InitializeComponent();
urlSearchViewModel = new UrlSearchViewModel();
}
}
ViewModel 代码:
public RelayCommand _GetVideo(string url)
{
return new RelayCommand(() => this.getVideo(url));
}
private void getVideo(string url)
{
try
{
FileInfo mp4 = youtubeConverter.DownloadVideoAsync(url, YoutubeConverter.TemporaryFolder).GetAwaiter().GetResult();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
例如,忽略愚蠢的异步部分。问题是url 始终是一个空字符串,所以每次按Button 时我都会收到Exception。
【问题讨论】:
-
CommandParameter="{x:Bind YoutubeUrlText.Text,Mode=OneWay}"将此添加到Button中,仅此Command="{x:Bind urlSearchViewModel._GetVideo}" -
@RaoHammas 这样会导致 Visual Studio 出现“找不到属性”。我认为您在谈论
Binding而不是x:Bind。顺便说一句,我试过了,它不起作用