0

我在 blend 中为我的应用程序创建了一个自定义按钮,并在我的 xaml 代码中使用了它的资源。但我想在我的课 xyztextbutton 中使用它。但不能这样。

[ button.Style = (Style)Application.Current.Resources["ButtonStyle1"];] 无法正常工作

我稍后将其添加到布局根目录中,但仍然无法正常工作。

电话:电话应用程序页面

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

     }

}
4

1 回答 1

0

据我所知,它应该工作。

好的,改变整个方法怎么样?

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

MyButton a = new MyButton();

于 2013-08-30T13:22:19.500 回答