简体   繁体   English

使用带有空 NavigateUri 的 HyperlinkButton 时崩溃

[英]Crash when using HyperlinkButton with empty NavigateUri

I am trying to use HyperlinkButton in my uwp app.我正在尝试在我的 uwp 应用程序中使用 HyperlinkButton。 And I am setting “NavigateUri” later in the lifecycle of the control.我在控件的生命周期后期设置“NavigateUri”。 Default value of “ViewModel.SecondaryLink” in the below code snippet is empty.以下代码片段中“ViewModel.SecondaryLink”的默认值为空。 And when it is empty, it is crashing.当它为空时,它正在崩溃。 So can we not keep the value of NavigateUri as empty for HyperlinkButton?那么我们可以不将 NavigateUri 的值保留为 HyperlinkButton 的空吗? When I initialize it in the constructor of control, it works without crash but I am getting this value from internet so I need to set it later.当我在控件的构造函数中初始化它时,它不会崩溃,但我是从互联网上获取这个值的,所以我需要稍后设置它。 Please help.请帮忙。

<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>

Crash when using HyperlinkButton with empty NavigateUri使用带有空 NavigateUri 的 HyperlinkButton 时崩溃

If you use Uri type to replace string type, it will not throw exception when apply null value.如果使用 Uri 类型替换字符串类型,应用 null 值时不会抛出异常。

public Uri SecondaryLink { get; set; } 

If you do want to use string type, please set default value when SecondaryLink is empty string.如果您确实要使用字符串类型,请在 SecondaryLink 为空字符串时设置默认值。

 public string SecondaryLink
 {

     get
     {
         if(_secondaryLink == null)
         {
             return "http://defaut";
         }
         else
         {
             return _secondaryLink;
         }

       
     }
     set
     {
         _secondaryLink = value;

     }
 }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM