[英]How can I change image.source with C# when creating a UWP app?
I'm developing my first Windows 10 UWP app. 我正在开发我的第一个Windows 10 UWP应用程序。 I have an image.
我有一张图片。 This is its XAML code:
这是它的XAML代码:
<Image x:Name="image"
HorizontalAlignment="Left"
Height="50"
Margin="280,0,0,25"
VerticalAlignment="Bottom"
Width="50"
Source="Assets/Five.png"/>
And im trying to change image.source with this code: 我试图用这段代码改变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;
}
}
But when I compile the code I get this error pointing to the variable declaration: 但是当我编译代码时,我得到了指向变量声明的错误:
UriFormatException was unhandled by user code
UriFormatException未被用户代码处理
Windows Runtime APIs do not support URIs of type UriKind.Relative , so you typically use the signature that infers the UriKind and make sure you've specified a valid absolute URI including the scheme and authority.
Windows运行时API不支持UriKind.Relative类型的URI,因此您通常使用可以推断UriKind的签名,并确保您指定了有效的绝对URI,包括方案和权限。
To access files stored inside the application package, but from code where there is no inferred root authority, specify the ms-appx: scheme like following: 要访问存储在应用程序包中的文件,但是从没有推断根权限的代码中,请指定ms-appx: scheme,如下所示:
BitmapImage One = new BitmapImage(new Uri("ms-appx:///Assets/One.png"));
For more info, please see How to load file resources (XAML) and URI schemes . 有关详细信息,请参阅如何加载文件资源(XAML)和URI方案 。
您需要为每个URI对象指定其他UriKind参数,以便将它们定义为相对,例如
new Uri("Assets/One.png", UriKind.Relative)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.