【问题标题】:Crash when using HyperlinkButton with empty NavigateUri使用带有空 NavigateUri 的 HyperlinkBut​​ton 时崩溃
【发布时间】:2022-01-19 00:39:35
【问题描述】:

我正在尝试在我的 uwp 应用程序中使用 HyperlinkBut​​ton。我在控件的生命周期后期设置“NavigateUri”。以下代码 sn-p 中“ViewModel.SecondaryLink”的默认值为空。当它为空时,它正在崩溃。那么我们可以不将 NavigateUri 的值保留为 HyperlinkBut​​ton 的空吗?当我在控件的构造函数中初始化它时,它不会崩溃,但我是从互联网上获取这个值的,所以我需要稍后设置它。请帮忙。

<Grid
    Grid.Column="1"
    CornerRadius="3"
    Margin="32,0,0,0">
    <HyperlinkButton
        Content="Learn more"
        FontSize="14"
        Margin="0,0,0,0"
        Style="{StaticResource HyperlinkButtonStyle}"
        NavigateUri="{x:Bind ViewModel.SecondaryLink, Mode=OneWay}" />
</Grid>

.cpp 文件

Windows::Foundation::Uri FamilyValuePropControlViewModel::SecondaryLink()
{
    return Windows::Foundation::Uri(m_secondaryLink);
}

.h 文件

winrt::hstring m_secondaryLink{ L"" };

【问题讨论】:

    标签: xaml uwp winrt-xaml uwp-xaml


    【解决方案1】:

    使用带有空 NavigateUri 的 HyperlinkBut​​ton 时崩溃

    如果使用Uri类型替换字符串类型,应用空值时不会抛出异常。

    public Uri SecondaryLink { get; set; } 
    

    如果要使用字符串类型,请在SecondaryLink为空字符串时设置默认值。

     public string SecondaryLink
     {
    
         get
         {
             if(_secondaryLink == null)
             {
                 return "http://defaut";
             }
             else
             {
                 return _secondaryLink;
             }
    
           
         }
         set
         {
             _secondaryLink = value;
    
         }
     }
    

    【讨论】:

    • 谢谢@Nico。我使用 Uri 作为类型,它解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多