我有2个xaml页面并尝试在1上选择一个图像,并将其设置在另一个页面上。这一切在Android上工作正常,但在Windows手机上产生错误。 我使用Plugin.media插件来选择图像。(https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Media)
我有这个功能来选择图像:
async void OnPictoChoose(object sender, EventArgs args)
{
if (!removing)
{
//create the new image and entry and the stacklayout to hold them
Image chosenImage = (Image)sender;
Label chosenLabel = new Label ();
StackLayout currentStack = new StackLayout ();
//find the stacklayout that holds the chosen image and store in currentStack, we need this to find the correct label/entry
foreach (StackLayout stack in pictoTheek.Children)
{
foreach (var child in stack.Children.OfType<Image>())
{
if (child == chosenImage)
{
currentStack = stack;
}
}
}
//find the entry that belongs to the image in the currentStack and store it in chosenLabel
foreach (var child in currentStack.Children.OfType<Label>())
{
chosenLabel = child;
}
//find the root page at the top of the navigationStack
MyPage stepPage = (MyPage)Navigation.NavigationStack [0];
//set the image and label with the method on the steppage using the chosenImage and chosenLabel
stepPage.SetImageAndLabel (chosenImage, chosenLabel);
//close the pictotheek
await Navigation.PopAsync ();
}
你可以看到我在steppage上调用SetImageAndLabel,如下所示:
public void SetImageAndLabel(Image pictotheekImg, Label pictptheekLabel)
{
//create stacklayout as placeholder to find the Label
StackLayout currentStack = new StackLayout();
//find the currentstack by looking for the selectedimage in the scrollsteps,
foreach (StackLayout child in scrollSteps.Children)
{
//set the selected image to the image chosen in the pictotheek
foreach (Image item in child.Children.OfType<Image>())
{
if (item == selectedImage)
{
currentStack = child;
item.BackgroundColor = Color.Transparent;
item.Source = pictotheekImg.Source;
}
}
//find the label that belongs to the selectedimage in the currentStack and set the text
foreach (Label label in currentStack.Children.OfType<Label>())
{
label.Text = pictptheekLabel.Text;
}
}
}
问题出在行item.Source = pictotheekImg.Source中,它产生:'this.MainWindow'抛出了'System.NotSupportedException'类型的异常。
内部异常sais“”null“,它确实有一条sais消息: “超出浏览器特定设置不会影响浏览器内应用程序”
第1页
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Lisa.Ruben.MyPage">
<ContentPage.Content>
<StackLayout>
<ScrollView Orientation="Horizontal" VerticalOptions="FillAndExpand">
<StackLayout Orientation = "Horizontal" VerticalOptions="FillAndExpand" BackgroundColor="White">
<StackLayout Orientation = "Horizontal" BackgroundColor="Transparent" x:Name="scrollSteps" Padding="31,0,31,0" VerticalOptions="Center">
<!-- dynamicly added images come here -->
</StackLayout>
</StackLayout>
</ScrollView>
<StackLayout Orientation="Horizontal" BackgroundColor="Silver" HeightRequest="75" >
<Button Text = "Add New Step" HorizontalOptions="End" Clicked="AddNewStep" WidthRequest="200"/>
<Button Text = "Remove Step" Clicked="OnRemoveButtonClick" WidthRequest="200" x:Name="removeButton"/>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
第2页:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Lisa.Ruben.PictotheekPage">
<ContentPage.Content>
<StackLayout>
<ScrollView Orientation="Horizontal" VerticalOptions="CenterAndExpand" x:Name="pictoScroll">
<StackLayout Orientation = "Horizontal" BackgroundColor="White" VerticalOptions="CenterAndExpand" Padding="15,15,0,20" x:Name="pictoTheek">
<StackLayout>
<Image Source="alien.jpg" WidthRequest="260" HeightRequest="226" VerticalOptions="Center">
<Image.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="OnPictoChoose"/>
</Image.GestureRecognizers>
</Image>
<Label Text="alien" BackgroundColor="Black" TextColor="White" VerticalOptions="Center" HorizontalTextAlignment="Center" />
</StackLayout>
<StackLayout>
<Image Source="levelup.jpg" WidthRequest="260" HeightRequest="226" VerticalOptions="Center" >
<Image.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="OnPictoChoose"/>
</Image.GestureRecognizers>
</Image>
<Label Text="level up" BackgroundColor="Black" TextColor="White" VerticalOptions="Center" HorizontalTextAlignment="Center"/>
</StackLayout>
<StackLayout>
<Image Source="bloem.jpg" WidthRequest="260" HeightRequest="226" VerticalOptions="Center" >
<Image.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="OnPictoChoose"/>
</Image.GestureRecognizers>
</Image>
<Label Text="bloem" BackgroundColor="Black" TextColor="White" VerticalOptions="Center" HorizontalTextAlignment="Center"/>
</StackLayout>
<StackLayout>
<Image Source="mario.jpg" WidthRequest="260" HeightRequest="226" VerticalOptions="Center">
<Image.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="OnPictoChoose"/>
</Image.GestureRecognizers>
</Image>
<Label Text="mario" BackgroundColor="Black" TextColor="White" VerticalOptions="Center" HorizontalTextAlignment="Center"/>
</StackLayout>
<StackLayout>
<Image Source="chrome.jpg" WidthRequest="260" HeightRequest="226" VerticalOptions="Center">
<Image.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="OnPictoChoose"/>
</Image.GestureRecognizers>
</Image>
<Label Text="chrome" BackgroundColor="Black" TextColor="White" VerticalOptions="Center" HorizontalTextAlignment="Center" />
</StackLayout>
<StackLayout>
<Image Source="circle.jpg" WidthRequest="260" HeightRequest="226" VerticalOptions="Center">
<Image.GestureRecognizers>
<TapGestureRecognizer NumberOfTapsRequired="1" Tapped="OnPictoChoose"/>
</Image.GestureRecognizers>
</Image>
<Label Text="circle" BackgroundColor="Black" TextColor="White" VerticalOptions="Center" HorizontalTextAlignment="Center"/>
</StackLayout>
</StackLayout>
</ScrollView>
<StackLayout Orientation="Horizontal" BackgroundColor="Silver" HeightRequest="75" x:Name="buttonBar">
<Button Text = "Add New Picto" Clicked="AddNewPicto" WidthRequest="150" VerticalOptions="Center"/>
<Button Text = "Create New Picto" Clicked="CreateNewPicto" WidthRequest="150" VerticalOptions="Center"/>
<Button Text = "Remove Picto" Clicked="OnRemovePictoClick" WidthRequest="150" VerticalOptions="Center" x:Name="removePictoButton"/>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>