简体   繁体   中英

Crash when using HyperlinkButton with empty NavigateUri

I am trying to use HyperlinkButton in my uwp app. And I am setting “NavigateUri” later in the lifecycle of the control. Default value of “ViewModel.SecondaryLink” in the below code snippet is empty. And when it is empty, it is crashing. So can we not keep the value of NavigateUri as empty for 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

If you use Uri type to replace string type, it will not throw exception when apply null value.

public Uri SecondaryLink { get; set; } 

If you do want to use string type, please set default value when SecondaryLink is empty string.

 public string SecondaryLink
 {

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

       
     }
     set
     {
         _secondaryLink = value;

     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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