Sam*_*son 8 c# xaml win-universal-app
我正在开发我的第一个Windows 10 UWP应用程序.我有一张图片.这是它的XAML代码:
<Image x:Name="image"
HorizontalAlignment="Left"
Height="50"
Margin="280,0,0,25"
VerticalAlignment="Bottom"
Width="50"
Source="Assets/Five.png"/>
Run Code Online (Sandbox Code Playgroud)
我试图用这段代码改变image.source:
private void slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
BitmapImage One = new BitmapImage(new Uri(@"Assets/One.png"));
BitmapImage Two = new BitmapImage(new Uri(@"Assets/Two.png"));
BitmapImage Three = new BitmapImage(new Uri(@"Assets/Three.png"));
BitmapImage Four = new BitmapImage(new Uri(@"Assets/Four.png"));
BitmapImage Five = new BitmapImage(new Uri(@"Assets/Five.png"));
if (slider.Value == 1)
{
image.Source = One;
}
else if (slider.Value == 2)
{
image.Source = Two;
}
else if (slider.Value == 3)
{
image.Source = Three;
}
else if (slider.Value == 4)
{
image.Source = Four;
}
else if (slider.Value == 5)
{
image.Source = Five;
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我编译代码时,我得到了指向变量声明的错误:
UriFormatException未被用户代码处理
小智 7
Windows运行时API不支持UriKind.Relative类型的URI ,因此您通常使用可以推断UriKind的签名,并确保您指定了有效的绝对URI,包括方案和权限.
要访问存储在应用程序包中的文件,但是从没有推断根权限的代码中,请指定ms-appx: scheme,如下所示:
BitmapImage One = new BitmapImage(new Uri("ms-appx:///Assets/One.png"));
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅如何加载文件资源(XAML)和URI方案.
小智 0
您需要为每个 URI 对象指定附加的 UriKind 参数,以便将它们定义为relative,例如
new Uri("Assets/One.png", UriKind.Relative)
Run Code Online (Sandbox Code Playgroud)