我在混合中设计了一个自定义样式,并希望在我的c#代码中使用它。怎么实现呢?

时间:2013-08-30 12:25:06

标签: c# windows-phone-7

我在混合中为我的应用程序创建了一个自定义按钮,并在我的xaml代码中使用了它的资源。 但我想在我的类xyztextbutton中使用它。 但是无法如此。

[button.Style =(Style)Application.Current.Resources [“ButtonStyle1”];]  工作不正常

我稍后会将它添加到布局根目录中,但仍无效。

电话:的PhoneApplicationPage

x:Class="xyz.MainPage"
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"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"    
shell:SystemTray.IsVisible="True" xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit">

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="ButtonStyle1" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ColorAnimation Duration="0" To="#FF216391" Storyboard.TargetProperty="(Shape.Stroke).(SolidColorBrush.Color)" Storyboard.TargetName="path" d:IsOptimized="True"/>
                                        <ColorAnimation Duration="0" To="#FF3E8EBB" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="path" d:IsOptimized="True"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="Disabled"/>
                                <VisualState x:Name="MouseOver"/>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Path x:Name="path" Data="M22.729,0 L22.73,0 L107.729,0 L107.729,38 L22.76,38 L22.76,38.031 L-0.483128,19.5199 L22.729,0.000793 z" Stretch="Fill" UseLayoutRounding="False" Stroke="#FFD0E21D" StrokeThickness="2" Fill="#FF5FB4E4" Margin="-0.483,0,0,0"/>
                        <ContentPresenter HorizontalAlignment="Center" Margin="24,1,4,9" Width="80" Height="30" OpacityMask="Black" VerticalAlignment="Top"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

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

public class xyzTextButton

{

    private int ScreenWidth = Convert.ToInt32(Application.Current.Host.Content.ActualWidth);
    private int ScreenHeight = Convert.ToInt32(Application.Current.Host.Content.ActualHeight);
    Grid uiGrid = null;
    Image image = null;
    TextBlock textblock = null;
    Button button = null;
    Dictionary<string, object> actions = null;

    public void CreateTextButton(Dictionary<string, object> dict, Grid grid, string pagename)

    {

        uiGrid = new Grid();
        image = new Image();
        button = new Button();
        button.Style = (Style)Application.Current.Resources["ButtonStyle1"];
        textblock = new TextBlock();
        Boolean autoresizesSubviews = Convert.ToBoolean(ObjectForKey("autoresizesSubviews", dict));
        actions = (Dictionary<string, object>)ObjectForKey("actions", dict);

     }

}

1 个答案:

答案 0 :(得分:0)

据我所知它应该有用。

好的,如何更改整个方法?

如何开发自定义用户控件,只需复制粘贴XAML,将其命名为MyButton,并用作普通按钮:

MyButton a = new MyButton();