如何使用 MVVM 和 Xamarin 在 XAML 中动态更改图像源

Leo*_*lho 5 c# xaml image xamarin xamarin.forms

我正在尝试更改ContentPage. I\xc2\xb4m 使用绑定上下文来执行此操作。但是,即使我更改模型视图中的源,这也不会更新我视图中的图像。

\n\n
UpdateMethod()\n{\n    imageSource1 = imageSource[1];\n}\n\npublic string ImageSource1\n{\n    get\n    {\n        return imageSource1;\n    }\n\n    set\n    {\n        imageSource1 = value;\n        this.Notify("ImageSource1");\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

XAML:

\n\n
<ContentView HorizontalOptions="Center" Grid.Row="0" >\n    <Image ClassId = "1" Source="{Binding ImageSource1}" BindingContextChanged="Handle_BindingContextChanged">\n        <Image.GestureRecognizers>\n            <TapGestureRecognizer  Command="{Binding OnTapGestureRecognizerTappedCommand1}" NumberOfTapsRequired="1" />\n        </Image.GestureRecognizers>\n    </Image>            \n</ContentView>\n
Run Code Online (Sandbox Code Playgroud)\n

Roh*_*ews 1

当您绑定时,ImageSource请使用Xamarin.Forms.ImageSource作为属性的返回类型。或者您可以使用它的派生类,就像FileImageSource指定文件路径一样。还要确保该路径存在于本机项目中。