UWP - 命令参数为空
UWP - Command parameters are empty
我正在使用 Microsoft 在其 ICommand
example 中实现的 RelayCommand
。这是我的代码的简要说明。
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();
}
}
视图模型代码:
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
。
UWP - Command parameters are empty
恐怕您无法将命令与包含参数且 return 类型为 RelayCommand
的方法绑定。请参考以下代码并编辑您的命令并使用 CommandParameter
属性.
传递参数
public class UrlSearchViewModel
{
public ICommand GetVideo
{
get
{
return new CommadEventHandler<string>((s) => this.getVideo(s));
}
}
private void getVideo(string url)
{
try
{
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
public class CommadEventHandler<T> : ICommand
{
public event EventHandler CanExecuteChanged;
public Action<T> action;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
this.action((T)parameter);
}
public CommadEventHandler(Action<T> action)
{
this.action = action;
}
}
Xaml
<Grid Background="GreenYellow">
<TextBox
x:Name="YoutubeUrlText"
Width="450"
VerticalAlignment="Top"
FontSize="20"
FontStyle="Italic"
PlaceholderText="Insert link from youtube here... "
/>
<Button
x:Name="ConvertButton"
Grid.Column="1"
Height="40"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="#FF0096FF"
BorderBrush="#FF797979"
Command="{x:Bind urlSearchViewModel.GetVideo}"
CommandParameter="{Binding ElementName=YoutubeUrlText,Path=Text}"
Content=""
FontFamily="Segoe MDL2 Assets"
FontSize="20"
Foreground="White"
/>
</Grid>
我正在使用 Microsoft 在其 ICommand
example 中实现的 RelayCommand
。这是我的代码的简要说明。
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();
}
}
视图模型代码:
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
。
UWP - Command parameters are empty
恐怕您无法将命令与包含参数且 return 类型为 RelayCommand
的方法绑定。请参考以下代码并编辑您的命令并使用 CommandParameter
属性.
public class UrlSearchViewModel
{
public ICommand GetVideo
{
get
{
return new CommadEventHandler<string>((s) => this.getVideo(s));
}
}
private void getVideo(string url)
{
try
{
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
public class CommadEventHandler<T> : ICommand
{
public event EventHandler CanExecuteChanged;
public Action<T> action;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
this.action((T)parameter);
}
public CommadEventHandler(Action<T> action)
{
this.action = action;
}
}
Xaml
<Grid Background="GreenYellow">
<TextBox
x:Name="YoutubeUrlText"
Width="450"
VerticalAlignment="Top"
FontSize="20"
FontStyle="Italic"
PlaceholderText="Insert link from youtube here... "
/>
<Button
x:Name="ConvertButton"
Grid.Column="1"
Height="40"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="#FF0096FF"
BorderBrush="#FF797979"
Command="{x:Bind urlSearchViewModel.GetVideo}"
CommandParameter="{Binding ElementName=YoutubeUrlText,Path=Text}"
Content=""
FontFamily="Segoe MDL2 Assets"
FontSize="20"
Foreground="White"
/>
</Grid>