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>
.cpp file
Windows::Foundation::Uri FamilyValuePropControlViewModel::SecondaryLink()
{
return Windows::Foundation::Uri(m_secondaryLink);
}
.h file
winrt::hstring m_secondaryLink{ L"" };
CodePudding user response:
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;
}
}