2015-11-01 60 views
2

我建立一个使用MVVM的应用程序,我试图命令绑定到所看到本教程的按钮:无法将命令绑定到一个HyperlinkBut​​ton(的Windows Phone 8.1)

https://channel9.msdn.com/Series/Windows-Phone-8-1-Development-for-Absolute-Beginners/Part-24-Binding-to-Commands-and-CommandParameters

然而,我无法使绑定工作 - 当我单击HyperlinkBut​​ton并且我在Execute方法的开头放置的断点不会触发时,没有任何反应。

这里是我的ViewModel:

public class ArticleListViewModel 
{ 
    public IEnumerable<ArticleItem> Items { get; set; } 

    public async Task PopulateArticles() 
    { 
     // ... 
    } 
} 


public class ArticleItem 
{ 
    public ArticleItem() 
    { 
     OpenLinkCommand = new OpenArticleLinkCommand(); 
    } 

    public ICommand OpenLinkCommand; 

    public string Url { get; set; } 

    public string Title { get; set; } 
} 

OpenArticleLinkCommand.cs:

public class OpenArticleLinkCommand : ICommand 
{ 
    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public event EventHandler CanExecuteChanged; 

    public async void Execute(object parameter) 
    { 
     var article = parameter as ArticleItem; 

     if (article != null) 
     { 
      var success = await Launcher.LaunchUriAsync(new Uri(article.Url)); 

      //TODO: handle success/fail 
     } 
    } 
} 

这里是我查看样子:

<UserControl 
    x:Class="LTNewsFeed.Views.ArticleItemView" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:LTNewsFeed.Views" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="300" 
    d:DesignWidth="400"> 


    <ListBox ItemsSource="{Binding}" x:Name="mainListBox"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 

        <HyperlinkButton x:Name="Title" 
            Content="{Binding Path=Title, Mode=OneWay}" 
            Command="{Binding OpenLinkCommand}" 
            Grid.Column="0"/> 

       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

</UserControl> 

主要Page.xaml:

<Page 
    x:Class="LTNewsFeed.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:LTNewsFeed" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:views="using:LTNewsFeed.Views" 
    xmlns:src="using:LTNewsFeed" 
    mc:Ignorable="d" 
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 

    <Grid> 
     <views:ArticleItemView x:Name="ItemsOnPage" /> 
    </Grid> 
</Page> 

我填充视图模型项集合在MainPage.xaml.cs:

protected override async void OnNavigatedTo(NavigationEventArgs e) 
    { 
     base.OnNavigatedTo(e); 

     await viewModel.PopulateArticles(); 

     ItemsOnPage.DataContext = viewModel.Items; 
    } 

事情我已经没有成功的尝试:

  • 引用了作为建议DataContext并指定ElementNamethis answer
  • 将HyperlinkBut​​ton更改为简单按钮。
  • OpenArticleLinkCommand移动到与我的ArticleItem模型相同的文件中,以防命名空间无法识别。
  • 在Google和StackOverflow上浏览类似的问题 - 在我看来,我所做的一切都与示例中的相同。

任何帮助指向我在正确的方向,或描述如何我可以调试这种东西将不胜感激。

+0

您是否看到列表框中的项目? –

+0

我怀疑你的演员在执行功能失败 –

回答

1

尽管听起来很奇怪,但绑定仅适用于属性。所以,你需要暴露你的命令作为一个属性,而不是一个领域:

public ICommand OpenLinkCommand { get; set; } 

你可以通过查看Visual Studio中的输出面板,其中,在执行时被显示错误消息已经注意到了这一点

Error: BindingExpression path error: 'OpenLinkCommand' property not found

请注意,您将立即遇到另一个问题:您希望为您的命令指定一个参数,但您忘记设置一个参数。由于您显然期待ArticleItem作为参数,因此您可以简单地绑定datacontext:

<HyperlinkButton x:Name="Title" 
    Content="{Binding Path=Title, Mode=OneWay}" 
    Command="{Binding OpenLinkCommand}" 
    CommandParameter="{Binding}" 
    Grid.Column="0"/> 
+0

谢谢!这一直使我疯狂,而且我没有注意到这个命令必须作为一个财产暴露出来。至于参数,我不小心把它弄乱了,而试图让绑定工作,但注意到好工作。感谢您关于输出面板的评论,从现在开始,我将确保在调试时随时打开它。 – hakas