如何从静态变量xamarin.forms绑定图像

时间:2016-06-10 11:31:36

标签: mvvm xamarin xamarin.android xamarin.forms mvvm-light

我必须绑定上传的图片,以便在导航到其他页面时不会更改。请告诉我如何将我的图像与静态变量绑定,以便它不会改变。

  public class demopage : ContentPage
{

    public int totalYears;
    ImageSource im;
    public ListView Menu { get; set; }
    public demopage()
    {
      BindingContext = App.viewmolel;
        Title = "Welcome";

        var profile = new Image { };

        // profile.Source = im;
      //  profile.Source = "profile.png";
        profile.HorizontalOptions = LayoutOptions.StartAndExpand;
        profile.VerticalOptions = LayoutOptions.StartAndExpand;

          profile.SetBinding(Image.SourceProperty, "LabelText");
        var profiletap = new TapGestureRecognizer();

        profiletap.Tapped += async (s, e) =>
        {
            var file = await CrossMedia.Current.PickPhotoAsync();

            if (file == null)
                return;
            await DisplayAlert("File Location", file.Path, "OK");
            var filepath = file.Path;

            im = ImageSource.FromStream(() =>
            {
                var stream = file.GetStream();

                //file.Dispose();
                return stream;


            });
            App.viewmolel.LabelText = im;
      };

这里是wiewmodel ---

      public class WelcomeViewModel : ViewModelBase
{
    public WelcomeViewModel()
    {
        LabelText = "profile.png";
    }
    private ImageSource imageText;
    public ImageSource LabelText 
    { 
        get { return imageText; }
        set
        {
            imageText = value; 
            RaisePropertyChanged(() => LabelText); 
        }
    }
}

这是app.cs

     public class App : Application
{
    private static WelcomeViewModel _viewmolel;
    public static WelcomeViewModel viewmolel { get { return _viewmolel; }
        //set {
        //    _viewmolel = value;
        //}
    }
    public App()
    {

        _viewmolel = new WelcomeViewModel();
        var btn = new Button {Text ="Hello" };
        // The root page of your application
        MainPage = new NavigationPage(new RootPage());

    }

0 个答案:

没有答案