1.只有切換頁面

2.加上頁面傳值的功能

第一部份:切換頁面

同樣的先建立一個新的Silverlight專案

silverlight 窗体切换 与 窗体之间传值 的实现

分別建立兩個User Control,並且名命為PageSwitcher、Page2

silverlight 窗体切换 与 窗体之间传值 的实现 

建立完成的結果

silverlight 窗体切换 与 窗体之间传值 的实现

接著修改PageSwitcher.xaml.cs

silverlight 窗体切换 与 窗体之间传值 的实现 public partial class PageSwitcher : UserControl
    }

然後現在要做第一頁設計頁的部分(Page.xaml)

silverlight 窗体切换 与 窗体之间传值 的实现 

<UserControl x:Class="SilverlightApplication2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel VerticalAlignment="Center">
        <TextBlock FontSize="50" Text="這是第一頁" HorizontalAlignment="Center" />
        <Button x:Name="GotoPage2" FontSize="30" Width="300" Content="我想去第二頁"></Button>
        </StackPanel>
    </Grid>
</UserControl>

而Page.xaml.cs程式碼如下

silverlight 窗体切换 与 窗体之间传值 的实现public partial class Page : UserControl
    }

然後是第二頁Page2的部份,基本上跟第一頁是一樣的

silverlight 窗体切换 与 窗体之间传值 的实现

Page2.xaml

<UserControl x:Class="SilverlightApplication2.Page2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel VerticalAlignment="Center">
            <TextBlock FontSize="50" Text="這是第二頁" HorizontalAlignment="Center" />
            <Button x:Name="GotoPage1" FontSize="30" Width="300" Content="我要回第一頁"></Button>
        </StackPanel>
    </Grid>
</UserControl>

Page2.xaml.cs

silverlight 窗体切换 与 窗体之间传值 的实现public partial class Page2 : UserControl
    }

最後把App.xaml.cs修改一下

silverlight 窗体切换 与 窗体之间传值 的实现 public App()
        }

這樣第一階段就完成了

第二部份:加入頁面傳值功能

雖然上面已做完換頁動作,但很多情況必須傳遞參數才能達到你要的目的,但是相對來說就比較麻煩一點了

建立一個新的Class,命名為Switcher.cs、ISwitchable.cs

silverlight 窗体切换 与 窗体之间传值 的实现

Switcher.cs

silverlight 窗体切换 与 窗体之间传值 的实现public static class Switcher
    }

ISwitchable.cs :這主要是要建立一個interface來共用並且傳值

在 namespace 裡有這段code就可以

silverlight 窗体切换 与 窗体之间传值 的实现    public interface ISwitchable
    }

再修改PageSwitcher.xml.cs

silverlight 窗体切换 与 窗体之间传值 的实现public partial class PageSwitcher : UserControl
    }

回去改第一頁的版面Page.xaml

silverlight 窗体切换 与 窗体之间传值 的实现 

<UserControl x:Class="SilverlightApplication2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel VerticalAlignment="Center">
        <TextBlock FontSize="50" Text="這是第一頁" HorizontalAlignment="Center" />
            <TextBlock FontSize="30" Text="你的名字:" Foreground="Blue"HorizontalAlignment="Center" />
            <TextBox FontSize="30" Width="300" x:Name="YourName"></TextBox>
                <Button x:Name="GotoPage2" Margin="20" FontSize="30" Width="300"Content="我想去第二頁"></Button>
        </StackPanel>
    </Grid>
</UserControl>

Page.xaml.cs

silverlight 窗体切换 与 窗体之间传值 的实现 public partial class Page : UserControl
    }

第二頁的版面Page2.xaml

silverlight 窗体切换 与 窗体之间传值 的实现 

<UserControl x:Class="SilverlightApplication2.Page2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel VerticalAlignment="Center">
            <TextBlock FontSize="50" Text="這是第二頁" HorizontalAlignment="Center" />
            <TextBlock FontSize="30" Text="我的名字是:" Foreground="Red"HorizontalAlignment="Center" />
            <TextBlock FontSize="30" x:Name="MyName" Foreground="Blue" HorizontalAlignment="Center" />
            <Button x:Name="GotoPage1" Margin="20" FontSize="30" Width="300"Content="我要回第一頁"></Button>
        </StackPanel>
    </Grid>
</UserControl>

 

Page2.xaml.cs

silverlight 窗体切换 与 窗体之间传值 的实现public partial class Page2 : UserControl, ISwitchable //要記得加上interface 才能繼承
    }

最後也是去修改

App.xaml.cs

silverlight 窗体切换 与 窗体之间传值 的实现public App()
        }

 

可以把PageSwitcher.xaml.cs再修改以方便除錯

silverlight 窗体切换 与 窗体之间传值 的实现public void Navigate(UserControl nextPage, object state)
        }

 

copyright http://www.dotblogs.com.tw/liuznsn/archive/2008/12/06/6276.aspx 

1.只有切換頁面

2.加上頁面傳值的功能

第一部份:切換頁面

同樣的先建立一個新的Silverlight專案

silverlight 窗体切换 与 窗体之间传值 的实现

分別建立兩個User Control,並且名命為PageSwitcher、Page2

silverlight 窗体切换 与 窗体之间传值 的实现 

建立完成的結果

silverlight 窗体切换 与 窗体之间传值 的实现

接著修改PageSwitcher.xaml.cs

silverlight 窗体切换 与 窗体之间传值 的实现 public partial class PageSwitcher : UserControl
    }

然後現在要做第一頁設計頁的部分(Page.xaml)

silverlight 窗体切换 与 窗体之间传值 的实现 

<UserControl x:Class="SilverlightApplication2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel VerticalAlignment="Center">
        <TextBlock FontSize="50" Text="這是第一頁" HorizontalAlignment="Center" />
        <Button x:Name="GotoPage2" FontSize="30" Width="300" Content="我想去第二頁"></Button>
        </StackPanel>
    </Grid>
</UserControl>

而Page.xaml.cs程式碼如下

silverlight 窗体切换 与 窗体之间传值 的实现public partial class Page : UserControl
    }

然後是第二頁Page2的部份,基本上跟第一頁是一樣的

silverlight 窗体切换 与 窗体之间传值 的实现

Page2.xaml

<UserControl x:Class="SilverlightApplication2.Page2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel VerticalAlignment="Center">
            <TextBlock FontSize="50" Text="這是第二頁" HorizontalAlignment="Center" />
            <Button x:Name="GotoPage1" FontSize="30" Width="300" Content="我要回第一頁"></Button>
        </StackPanel>
    </Grid>
</UserControl>

Page2.xaml.cs

silverlight 窗体切换 与 窗体之间传值 的实现public partial class Page2 : UserControl
    }

最後把App.xaml.cs修改一下

silverlight 窗体切换 与 窗体之间传值 的实现 public App()
        }

這樣第一階段就完成了

第二部份:加入頁面傳值功能

雖然上面已做完換頁動作,但很多情況必須傳遞參數才能達到你要的目的,但是相對來說就比較麻煩一點了

建立一個新的Class,命名為Switcher.cs、ISwitchable.cs

silverlight 窗体切换 与 窗体之间传值 的实现

Switcher.cs

silverlight 窗体切换 与 窗体之间传值 的实现public static class Switcher
    }

ISwitchable.cs :這主要是要建立一個interface來共用並且傳值

在 namespace 裡有這段code就可以

silverlight 窗体切换 与 窗体之间传值 的实现    public interface ISwitchable
    }

再修改PageSwitcher.xml.cs

silverlight 窗体切换 与 窗体之间传值 的实现public partial class PageSwitcher : UserControl
    }

回去改第一頁的版面Page.xaml

silverlight 窗体切换 与 窗体之间传值 的实现 

<UserControl x:Class="SilverlightApplication2.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel VerticalAlignment="Center">
        <TextBlock FontSize="50" Text="這是第一頁" HorizontalAlignment="Center" />
            <TextBlock FontSize="30" Text="你的名字:" Foreground="Blue"HorizontalAlignment="Center" />
            <TextBox FontSize="30" Width="300" x:Name="YourName"></TextBox>
                <Button x:Name="GotoPage2" Margin="20" FontSize="30" Width="300"Content="我想去第二頁"></Button>
        </StackPanel>
    </Grid>
</UserControl>

Page.xaml.cs

silverlight 窗体切换 与 窗体之间传值 的实现 public partial class Page : UserControl
    }

第二頁的版面Page2.xaml

silverlight 窗体切换 与 窗体之间传值 的实现 

<UserControl x:Class="SilverlightApplication2.Page2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel VerticalAlignment="Center">
            <TextBlock FontSize="50" Text="這是第二頁" HorizontalAlignment="Center" />
            <TextBlock FontSize="30" Text="我的名字是:" Foreground="Red"HorizontalAlignment="Center" />
            <TextBlock FontSize="30" x:Name="MyName" Foreground="Blue" HorizontalAlignment="Center" />
            <Button x:Name="GotoPage1" Margin="20" FontSize="30" Width="300"Content="我要回第一頁"></Button>
        </StackPanel>
    </Grid>
</UserControl>

 

Page2.xaml.cs

silverlight 窗体切换 与 窗体之间传值 的实现public partial class Page2 : UserControl, ISwitchable //要記得加上interface 才能繼承
    }

最後也是去修改

App.xaml.cs

silverlight 窗体切换 与 窗体之间传值 的实现public App()
        }

 

可以把PageSwitcher.xaml.cs再修改以方便除錯

silverlight 窗体切换 与 窗体之间传值 的实现public void Navigate(UserControl nextPage, object state)
        }

 

copyright http://www.dotblogs.com.tw/liuznsn/archive/2008/12/06/6276.aspx 

相关文章: