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

Link Untuk Belajar

♠ Posted by Unknown in

Link Untuk Belajar

http://msdn.microsoft.com/en-us/library/zd5e2z84.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

Windows Phone || Contoh Simple Binding List

♠ Posted by Unknown in

Contoh Simple Binding List Windows Phone

MainPage.xaml
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Command="{Binding Tambah}" Content="Button" HorizontalAlignment="Left" Margin="336,-126,0,0" VerticalAlignment="Top"/>
            <ListBox ItemsSource="{Binding L_classnama}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Width="400" Background="Goldenrod" Margin="0,0,0,5">
                            <!-- Binding Source Gambar -->
                            <Image Margin="20,0,0,0" Height="60" Width="60" Source="{Binding Gambar}"></Image>
                            <!-- Binding TextBlock -->
                            <TextBlock Text="{Binding Nama}" TextWrapping="Wrap" FontSize="30" Style="{StaticResource PhoneTextExtraLargeStyle}" FontFamily="Comic Sans MS"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>

MainPage.xaml.cs

public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            DataContext = new MAinPageViewModle();
        }
    }


MAinPageViewModle

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
namespace Bi
{
    class MAinPageViewModle : Microsoft.Practices.Prism.ViewModel.NotificationObject
    {
        public List<ClassNama> L_classnama{get;set;}
        private Microsoft.Practices.Prism.Commands.DelegateCommand _tambah;
        void TambahAction() {
            L_classnama = new List<ClassNama>();
            L_classnama.Add(new ClassNama() { Nama = "xxxx", Gambar = "unduhan.jpg" });
            L_classnama.Add(new ClassNama() { Nama = "xx", Gambar = "unduhan.jpg" });
            L_classnama.Add(new ClassNama() { Nama = "xxx", Gambar = "unduhan.jpg" });
            Console.WriteLine("asssssssssssssssssssssssssssss");
            RaisePropertyChanged("");
        }
        public Microsoft.Practices.Prism.Commands.DelegateCommand Tambah
        {
            get { return _tambah; }
        }
        public MAinPageViewModle(){
            _tambah = new Microsoft.Practices.Prism.Commands.DelegateCommand(TambahAction);
            L_classnama = new List<ClassNama>();
            L_classnama.Add(new ClassNama() { Nama = "asdasdas" ,Gambar="unduhan.jpg"});
            L_classnama.Add(new ClassNama() { Nama = "asdasdas", Gambar = "unduhan.jpg" });
            L_classnama.Add(new ClassNama() { Nama = "asdasdas", Gambar = "unduhan.jpg" });
            L_classnama.Add(new ClassNama() { Nama = "asdasdas", Gambar = "unduhan.jpg" });
            L_classnama.Add(new ClassNama() { Nama = "asdasdas", Gambar = "unduhan.jpg" });
            L_classnama.Add(new ClassNama() { Nama = "asdasdas", Gambar = "unduhan.jpg" });
        }
    }  
}
 
ClassNama

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bi
{
    class ClassNama  : Microsoft.Practices.Prism.ViewModel.NotificationObject
    {
        private string _nama;
        private string _gambar;
        public string Nama { get { return _nama; } set { _nama = value; RaisePropertyChanged(""); } }
        public string Gambar { get { return _gambar; } set { _gambar = value; RaisePropertyChanged(""); } }
    }
}





Dapatkan nama file dari url C#

♠ Posted by Unknown in

Dapatkan nama file dari url C#

public string GetFileName {             
            get{
                string URL = "http://vendyxiao.com/wp-content/uploads/2011/09/facebook_icon.png";
                _getNameFile = URL.Substring(URL.LastIndexOf("/") + 1, (URL.Length - URL.LastIndexOf("/") - 1));
                return _getNameFile;
            }
            set {
                _getNameFile = value;
            }
        }

Windows Phone || Save Image from Url

♠ Posted by Unknown in

Save Image from Url


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SaveImageHttptoIso2.Resources;
using System.Windows.Media.Imaging;
using System.IO;
using System.IO.IsolatedStorage;
using System.Threading;
using Microsoft.Xna.Framework.Media;
using System.Windows.Resources;

namespace SaveImageHttptoIso2
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Uri uri = new Uri("http://www.english.cam.ac.uk/pop/images/google_icon.png", UriKind.Absolute);
            xxx.Source = new BitmapImage(uri);

            var webClient = new WebClient();
            webClient.OpenReadCompleted += WebClientOpenReadCompleted;
            webClient.OpenReadAsync(new Uri("http://www.english.cam.ac.uk/pop/images/google_icon.png", UriKind.Absolute));
        }

        void WebClientOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            const string tempJpeg = "TempJPEG.jpg";
            var streamResourceInfo = new StreamResourceInfo(e.Result, null);

            var userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
            if (userStoreForApplication.FileExists(tempJpeg))
            {
                userStoreForApplication.DeleteFile(tempJpeg);
            }

            var isolatedStorageFileStream = userStoreForApplication.CreateFile(tempJpeg);

            var bitmapImage = new BitmapImage { CreateOptions = BitmapCreateOptions.None };
            bitmapImage.SetSource(streamResourceInfo.Stream);

            var writeableBitmap = new WriteableBitmap(bitmapImage);
            writeableBitmap.SaveJpeg(isolatedStorageFileStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 85);

            
    }
}

Simple Binding Image Windows Phone Isolatedstorage

♠ Posted by Unknown in

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));
            }
        }
    }
}






Windows Phone || INotifyPropertyChanged

♠ Posted by Unknown in

INotifyPropertyChanged


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace PhoneApp2
{
    class DataBinding2 : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private int _nama,_a,_b,_c;
        public int Nama {
            get {
                return _nama;
            }
            set {
                _nama = value;
                _nama = _nama * 5;
                berubah("Nama");
            }
        }

        public int A {
            get
            {
                return _a;
            }
            set
            {
                _a = value;
                berubah("");
            }
        }

        public int B
        {
            get
            {
                return _b;
            }
            set
            {
                _b = value;
                berubah("");
            }
        }

        public int C
        {
            get
            {
                return _c=_a+_b;
            }
            set
            {
                _c = value;
                berubah("");
            }
        }
        void berubah(string a) {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(a));
            }
        }
    }
}



------------------


<phone:PhoneApplicationPage
    x:Class="PhoneApp2.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="Button" HorizontalAlignment="Left" Margin="153,31,0,0" VerticalAlignment="Top" />
            <TextBox Text="{Binding Nama}" HorizontalAlignment="Left" Height="72" Margin="0,133,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="456"/>
            <TextBox HorizontalAlignment="Left" Height="72" Margin="0,205,0,0" TextWrapping="Wrap" Text="{Binding A, Mode=TwoWay}" VerticalAlignment="Top" Width="456"/>
            <TextBox HorizontalAlignment="Left" Height="72" Margin="0,265,0,0" TextWrapping="Wrap" Text="{Binding B, Mode=TwoWay}" VerticalAlignment="Top" Width="456"/>
            <TextBox HorizontalAlignment="Left" Height="72" Margin="0,342,0,0" TextWrapping="Wrap" Text="{Binding C, Mode=TwoWay}" VerticalAlignment="Top" Width="456"/>
        </Grid>
        <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </Grid>

</phone:PhoneApplicationPage>