我正在嘗試在我的 uwp 應用程式中使用 HyperlinkBut??ton。我將在控制元件生命周期的后期設定“NavigateUri”。以下代碼片段中“ViewModel.SecondaryLink”的默認值為空。當它是空的時,它就會崩潰。那么我們可以不讓 HyperlinkBut??ton 的 NavigateUri 的值保持為空嗎?當我在控制元件的建構式中初始化它時,它可以正常作業而不會崩潰,但是我是從 Internet 獲取這個值的,所以我需要稍后設定它。請幫忙。
<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"" };
uj5u.com熱心網友回復:
使用帶有空 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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/386138.html
標籤:xaml 超载 winrt-xaml uwp-xaml
