Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更改计时器的样式和颜色_C#_Xamarin.forms - Fatal编程技术网

C# 如何更改计时器的样式和颜色

C# 如何更改计时器的样式和颜色,c#,xamarin.forms,C#,Xamarin.forms,我正在Xamarin.Forms应用程序中努力处理样式和着色时间以及日期选择器。目前它是亮粉色(ugh),我正在拼命寻找在整个项目xaml文件“App.xaml”中更改它的可能性 我只知道如何给按钮的背景上色,而不知道对话框本身 ->我在这里也看到了这个问题:,但我不想在Android项目本身中改变它。我正在寻找一种定制这些对话框的全局方法 My App.xaml代码: <Application xmlns="http://xamarin.com/schemas/2014/for

我正在Xamarin.Forms应用程序中努力处理样式和着色时间以及日期选择器。目前它是亮粉色(ugh),我正在拼命寻找在整个项目xaml文件“App.xaml”中更改它的可能性 我只知道如何给按钮的背景上色,而不知道对话框本身

->我在这里也看到了这个问题:,但我不想在Android项目本身中改变它。我正在寻找一种定制这些对话框的全局方法

My App.xaml代码:

<Application xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Arbeitszeitrechner_Forms.App">
    <!--
        Define global resources and styles here, that apply to all pages in your app.
    -->
    <Application.Resources>
        <ResourceDictionary>
            <Color x:Key="Primary">#77C9D4</Color> <!-- Feather -->
            <Color x:Key="Feather">#77C9D4</Color>
            <Color x:Key="Marine">#57BC90</Color>
            <Color x:Key="Forest">#015249</Color>
            <Color x:Key="SleekGrey">#A5A5AF</Color>
            <Style TargetType="Button">
                <Setter Property="TextColor" Value="{StaticResource Marine}"></Setter>
                <Setter Property="VisualStateManager.VisualStateGroups">
                    <VisualStateGroupList>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor" Value="{StaticResource Forest}" />
                                </VisualState.Setters>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor" Value="{StaticResource Forest}" />
                                    <Setter Property="Opacity" Value="0.1"/>
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateGroupList>
                </Setter>
            </Style>
            <Style TargetType="TimePicker">
                <Setter Property="BackgroundColor" Value="{StaticResource Marine}"/>
                <Setter Property="Background" Value="{StaticResource Forest}"/>
            </Style>
            <Style TargetType="DatePicker">
                <Setter Property="BackgroundColor" Value="{StaticResource Marine}"/>
                <Setter Property="Background" Value="{StaticResource Forest}"/>
            </Style>
        </ResourceDictionary>        
    </Application.Resources>
</Application>

您可以在特定于平台的项目中更改style.xml文件中时间选择器的样式和颜色,如下图所示


您可以在其中编辑颜色和样式。有关详细信息,请查看。

在Xamarin.forms中,您可以使用自定义渲染器执行此操作

Android:

首先设置样式:

 <style name="TimePickerTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#039BE5</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

#039BE5
包装内容
包装内容
并在“自定义渲染器”对话框中设置此主题:

[assembly: ExportRenderer(typeof(Xamarin.Forms.TimePicker), typeof(TimePickerDialogCustomRenderer))]

namespace App1.Droid
{
class TimePickerDialogCustomRenderer : TimePickerRenderer
{
    private readonly Context _context;
    public TimePickerDialogCustomRenderer(Context context) : base(context)
    {
        _context = context;

    }
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
    {
        base.OnElementChanged(e);
    }

    TimePickerDialog dialog;
    protected override TimePickerDialog CreateTimePickerDialog(int hours, int minutes)
    {
        dialog = new TimePickerDialog(_context, Resource.Style.TimePickerTheme, this, hours, minutes, false);
         
        return dialog;

    }
}
}
[程序集:导出渲染器(typeof(Xamarin.Forms.TimePicker),typeof(TimePickerDialogCustomRenderer))]
名称空间App1.Droid
{
类TimePickerDialogCustomRenderer:TimePickerRenderer
{
私有只读上下文\u上下文;
公共时间选择器DialogCustomRenderer(上下文):基本(上下文)
{
_上下文=上下文;
}
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
}
时间选择器对话框;
受保护的覆盖时间选择器Dialog CreateTimePickerDialog(整数小时,整数分钟)
{
dialog=newtimepickerdialog(_context,Resource.Style.TimePickerTheme,this,hours,minutes,false);
返回对话框;
}
}
}

选择器是本机平台控件,因此您需要在每个平台项目中分别修改if谢谢提示!如果我想更改默认颜色,看起来我必须定制渲染器和特定于平台的解决方案。
[assembly: ExportRenderer(typeof(Xamarin.Forms.TimePicker), typeof(TimePickerDialogCustomRenderer))]

namespace App1.Droid
{
class TimePickerDialogCustomRenderer : TimePickerRenderer
{
    private readonly Context _context;
    public TimePickerDialogCustomRenderer(Context context) : base(context)
    {
        _context = context;

    }
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.TimePicker> e)
    {
        base.OnElementChanged(e);
    }

    TimePickerDialog dialog;
    protected override TimePickerDialog CreateTimePickerDialog(int hours, int minutes)
    {
        dialog = new TimePickerDialog(_context, Resource.Style.TimePickerTheme, this, hours, minutes, false);
         
        return dialog;

    }
}
}