简体   繁体   English

绑定HyperlinkBut​​ton Windows Phone 8的NavigateUri属性

[英]Bind NavigateUri property of HyperlinkButton Windows Phone 8

I am parsing some JSON data using Newtonsoft.NET for my WP8 app in C#/XAML and everything works fine except the binding of NavigationUri to the HyperlinkButton. 我正在使用Newtonsoft.NET在C#/ XAML中为我的WP8应用程序解析一些JSON数据,除了将NavigationUri绑定到HyperlinkBut​​ton之外,其他一切工作都很好。 Here is the DataTemplate of my databound LongListSelector: 这是我的数据绑定LongListSelector的DataTemplate:

<DataTemplate>
<StackPanel Margin="10 10 10 20" Background="{StaticResource PhoneAccentBrush}">
<TextBlock Text="{Binding MovieTitle }" TextWrapping="Wrap" FontSize="18" />
<TextBlock Text="{Binding ImdbCode }" TextWrapping="Wrap" FontSize="18" />
<HyperlinkButton NavigateUri="{Binding ImdbLink}">View on IMDB</HyperlinkButton>
<Image Source="{Binding MovieCover}" Width="300" Height="300" HorizontalAlignment="Center"/>
<TextBlock Text="{Binding ImdbLink}" TextWrapping="Wrap" FontSize="18" />
</StackPanel>
</DataTemplate>

This way everything is loaded correctly but the HyperLinkButton doesn't navigate to the Uri. 这样,所有内容都可以正确加载,但是HyperLinkBut​​ton不会导航到Uri。

What should I do to make sure the navigation works? 我应该怎么做才能确保导航正常? If it is not possible with HyperLinkButton then can I add a regular button to which I could pass the url so that OnClick event can navigate to url. 如果HyperLinkBut​​ton无法实现,那么我可以添加一个可以向其传递url的常规按钮,以便OnClick事件可以导航至url。

This is because you have no RequestNavigate method. 这是因为您没有RequestNavigate方法。 Try: 尝试:

<HyperlinkButton NavigateUri="{Binding ImdbLink}" RequestNavigate="Hyperlink_RequestNavigate">View on IMDB</HyperlinkButton>

and in your code behind for the view put 并在您后面的代码中放置视图

    private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
    {
        Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
        e.Handled = true;
    }

or however you want to deal with that click event (ie open a new webview with that as the url) 或者您想处理该点击事件(即以该URL作为URL打开一个新的Webview)

Assuming the url is correct the webbrowser can be activated like this: 假设URL是正确的,则可以按以下方式激活Web浏览器:

XAML XAML

<HyperlinkButton NavigateUri="{Binding ImdbLink}" 
                 Click="NavigateButton_Click">View on IMDB</HyperlinkButton> 

C# C#

private void NavigateButton_Click(object sender, RoutedEventArgs e)
{
    var hyperlinkButton = sender as HyperlinkButton;
    if(hyperlinkButton == null)
    {
        return;
    }

    ShowInBrowser(hyperlinkButton.NavigateUri);
}

private void ShowInBrowser(Uri url)
{
    Microsoft.Phone.Tasks.WebBrowserTask wbt = 
        new Microsoft.Phone.Tasks.WebBrowserTask();
    wbt.Uri = url;
    wbt.Show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将命令绑定到超链接按钮 (Windows Phone 8.1) - Unable to bind a Command to a HyperlinkButton (Windows Phone 8.1) 将Windows Phone HyperlinkBut​​ton导航移植到Windows 8 HyperlinkBut​​ton导航 - Porting Windows Phone HyperlinkButton Navigation To Windows 8 HyperlinkButton Navigation Windows Phone Dev 8更改HyperlinkBut​​ton图像(无SILVERLIGHT) - Windows Phone Dev 8 change HyperlinkButton Image (NO SILVERLIGHT) 创建HyperlinkBut​​ton,并在其后的代码中包含文字换行(Windows Phone 8) - Create HyperlinkButton with text wrapping in code behind (Windows Phone 8) 如何使用HyperlinkBut​​ton在Windows Phone 8.1中显示ContentDialog页面 - How to use HyperlinkButton to show ContentDialog page in Windows Phone 8.1 HyperLinkBut​​ton中的多个绑定导航URI - Multiple Bind in HyperLinkButton Navigate URI Windows Phone CustomControl数据绑定 - Windows Phone CustomControl Data Bind 如何在通用应用程序/ Windows 8.1和Windows Phone的所有页面上的App.xaml.cs上绑定属性? - How to bind a property on App.xaml.cs on all pages of universal app / Windows 8.1 and Windows Phone? Windows Phone与Listbbx绑定列表框 - Windows Phone Bind Listbox With a Listbbx 在Windows Phone 8.1中将“ IsEnabled”绑定到ListView - Bind “IsEnabled” to ListView in Windows Phone 8.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM