Belajardengan.com merupakan domain blog lama dari delajardengan.blogspot.com. Mohon kritik dan sarannya untuk perkembangan blog ini.
email: blog.tkx.pnp@gmail.com

Simple Binding Image Windows Phone Isolatedstorage

♠ Posted by Unknown in at 4:18 PM

Simple Binding Image Windows Phone Isolatedstorage


MainPage.xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox HorizontalAlignment="Left" Height="72" Margin="10,10,-10,0" TextWrapping="Wrap" Text="{Binding Nama, Mode=TwoWay}" VerticalAlignment="Top" Width="456"/>
            <TextBox HorizontalAlignment="Left" Height="72" Margin="10,82,-10,0" TextWrapping="Wrap" Text="{Binding Password, Mode=TwoWay}" VerticalAlignment="Top" Width="456"/>
            <Button Command="{Binding Save, Mode=TwoWay}" Content="Button" HorizontalAlignment="Left" Margin="158,178,0,0" VerticalAlignment="Top"/>
            <Button Content="Netxt" HorizontalAlignment="Left" Margin="187,311,0,0" VerticalAlignment="Top" Click="Button_Click_1"/>
        </Grid>

---
Page1.xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock HorizontalAlignment="Left" Margin="73,40,0,0" TextWrapping="Wrap" Text="{Binding Iso, Mode=OneTime}" VerticalAlignment="Top" Width="275"/>
            <Image Source="{Binding ThumbImage}" HorizontalAlignment="Left" Height="100" Margin="182,176,0,0" VerticalAlignment="Top" Width="100"/>
        </Grid>

---

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using System.IO;
using System.Windows.Resources;
using System.Windows;
namespace lainPageIso
{
    class MainViewModel : INotifyPropertyChanged
    {
        private string _nama, _password;

        private System.Windows.Input.ICommand _save;
 
        public MainViewModel(){
            this._save = new DelegateCommand(this.SaveAction);
        }

        public string Nama {
            get { return _nama; }
            set { _nama = value; RaisePropertyChanged(""); }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; RaisePropertyChanged(""); }
        }

        public void SaveAction(object p) {

            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
            StreamWriter streamWriter = null;
            if (!isf.FileExists("coba.txt"))
            {
                isf.CreateFile("coba.txt");
            }
            else {
                streamWriter = new StreamWriter(new IsolatedStorageFileStream("coba.txt",FileMode.Open,isf));
             
            }
            streamWriter.WriteLine(_nama+""+_password);
            streamWriter.Close();
            saveImage();
            RaisePropertyChanged("");
        }

        public System.Windows.Input.ICommand Save {
            get {
                return _save;
            }
        }

        void saveImage() {
            String tempJPEG = "logo.jpg";

            // Create virtual store and file stream. Check for duplicate tempJPEG files.
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIsolatedStorage.FileExists(tempJPEG))
                {
                    myIsolatedStorage.DeleteFile(tempJPEG);
                }

                IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);

                StreamResourceInfo sri = null;
                Uri uri = new Uri(tempJPEG, UriKind.Relative);
                sri = Application.GetResourceStream(uri);

                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(sri.Stream);
                WriteableBitmap wb = new WriteableBitmap(bitmap);

                // Encode WriteableBitmap object to a JPEG stream.
                Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);

                //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                fileStream.Close();
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

---

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using System.IO;
using System.ComponentModel;

namespace lainPageIso
{
    class Page1ViewModel : INotifyPropertyChanged
    {
        IsolatedStorageFile get = IsolatedStorageFile.GetUserStoreForApplication(); 
        string x   ="";

        public Page1ViewModel() {
            if (get.FileExists("coba.txt"))
            {
                try
                {
                    StreamReader read = new StreamReader(new IsolatedStorageFileStream("coba.txt", FileMode.Open, get));
                    x = read.ReadLine();
                    read.Close();
                }
                catch (Exception ex)
                {
                }

            }
            if (get.FileExists("logo.jpg"))
            {
                x += " image ada";
            }
        }
        public string Iso {         
            
            get { return x; } 
        }

        void readImage() {
            BitmapImage bi = new BitmapImage();

            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("logo.jpg", FileMode.Open, FileAccess.Read))
                {
                    //bi.SetSource(fileStream);
                    //img.Width = bi.PixelWidth;
                }
            }
            //this.img.Source = bi;
        }

        private string _thumbFileName;
        public string ThumbFileName
        {
            get
            {
                return _thumbFileName;
            }
            set
            {
                _thumbFileName = value;
                RaisePropertyChanged("ThumbFileName");
                RaisePropertyChanged("ThumbImage");
            }
        }

        public BitmapImage ThumbImage
        {
            get
            {
                BitmapImage image = new BitmapImage();
                IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
                string isoFilename = ThumbFileName;
                var stream = isoStore.OpenFile("logo.jpg", System.IO.FileMode.Open);
                image.SetSource(stream);
                return image;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}






0 komentar:

Post a Comment