首页 > 解决方案 > 使用 xamforms.controls.calendar 构建日历活动

问题描述

所以,像大多数人一样,我仍然很新,正在浏览所有 c# 和 Xamarin.Forms,但它的速度非常慢。使用 XamarinForms 和 XamForms.Controls.Calendar 插件,我正在尝试构建一个日历,该日历获取发生的日期和事件的列表,并在底部放置一个小的颜色代码。我查看了我能找到的所有源代码和主题,我已经对其进行了重构并构建了下面的代码版本,它可以满足我的需求,但它很丑......我想让它更可重用和更多 MVVM比他们之前使用的示例和我所做的示例。基本上我的计划是根据布尔值将颜色绑定到活动(应该能够在代码中看到)。所以,挑战在于我真的不知道去哪里或做什么,但下面是我认为需要发生的事情。

XamlPage

 <?xml version="1.0" encoding="UTF-8"?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:controls="clr-namespace:XamForms.Controls;assembly=XamForms.Controls.Calendar"
         x:Class="MyApp.View.LogPageViews.LogPage"
         ControlTemplate="{StaticResource MainPageTemplate}">
<ContentPage.Content>
    <ScrollView>
        <StackLayout>
            <controls:Calendar x:Name="calendar Padding="10,0,10,0" SelectedBorderWidth="4" DisabledBorderColor="Black" ShowNumberOfWeek="false" StartDay="Monday TitleRightArrowTextColor="Blue" TitleLabelTextColor="Purple" TitleLeftArrowTextColor="Blue" WeekdaysFontSize="12" SelectedDate="{Binding Date}" SpecialDates="{Binding Workout}" DateCommand="{Binding DateChosen}" >
            </controls:Calendar>
        </StackLayout>
    </ScrollView>
</ContentPage.Content>
</ContentPage>

我在我尝试过的所有代码中都遇到了错误......这可能是几个错误的事情......我什至不知道这是否是好的代码。我当前的错误是严重性代码描述项目文件行抑制状态错误 CS1503 参数 1:无法从 'MyApp.ViewModel.CalendarActivityVM' 转换为 'XamForms.Controls.SpecialDate'

namespace MyApp.View.LogPageViews
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LogPage : ContentPage
    {  
        public LogPage ()
        {
            InitializeComponent ();
                    var Dates = new List<SpecialDate>();
                    NavigationPage.SetHasNavigationBar(this, false);
                    DateTime testdate = new DateTime(2018, 06, 26);
    //This is where I am getting the errors when I try to do anything like this.  Getting errors like "Cannot convert from" and "Cannot use like a method" 
//I did have code here that worked, but refactored it out and got stuck         
            calendar.SpecialDates.Add(new CalendarActivityVM(testdate, true, true, true, true));
            calendar.SelectedDate = (DateTime.Now);
        }
    }
}

我的日历 ActivityVM(我不知道这是否正确......这是我正在学习并认为正确的地方)

namespace MyApp.ViewModel
{
public class CalendarActivityVM
{
    public DateTime Datetime { get; set; }
    public bool A { get; set; }
    public bool B { get; set; }
    public bool C { get; set; }
    public bool D { get; set; }

    public CalendarActivityVM(DateTime datetime, bool a, bool b, bool c, bool d)
    {
        Datetime = datetime;
        A = a;
        B = b;
        C = c;
        D = d;

        var AColor = new Color();
        var BColor = new Color();
        var CColor = new Color();
        var DColor = new Color();

        if (A == true) { AColor = Color.Red; } else { AColor = Color.Transparent; };
        if (B == true) { BColor = Color.Yellow; } else { BColor = Color.Transparent; };
        if (C == true) { CColor = Color.Green; } else { CColor = Color.Transparent; };
        if (D == true) { DColor = Color.Blue; } else { DColor = Color.Transparent; };

        var white_row = new Pattern { WidthPercent = .1275f, HightPercent = 0.75f, Color = Color.Transparent };
        var white_col = new Pattern { WidthPercent = 0.04f, HightPercent = 1f, Color = Color.Transparent };
        var aPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = AColor };
        var bPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = BColor };
        var cPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = CColor };
        var dPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = DColor };

        var activity = new BackgroundPattern(7)
        {
            Pattern = new List<Pattern>
                    {
                        white_row, white_row, white_row, white_row, white_row, white_row, white_row,
                            aPattern, white_col, bPattern, white_col, cPattern, white_col, dPattern
                    }
        };
    }
}
}

根据示例,正常的“SpecialDate”看起来像这样......这就是我试图重构的原因:

calendar.SpecialDates = new List<SpecialDate>{
                new SpecialDate(DateTime.Now.AddDays(3))
                {
                    BackgroundColor = Color.White,
                    TextColor = Color.Black,
                    Selectable = true,
                    BackgroundPattern = new BackgroundPattern(7)
                {
                    Pattern = new List<Pattern>
                        {
                        new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Red, Text = "X", TextColor=Color.White, TextSize=11, TextAlign=TextAlign.Middle},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Gold, Text = "Y", TextColor=Color.White, TextSize=11, TextAlign=TextAlign.Middle},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Green, Text = "Z", TextColor=Color.White, TextSize=11, TextAlign=TextAlign.Middle},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Purple,Text = "Q", TextColor=Color.White, TextSize=11, TextAlign=TextAlign.Middle},

                            white_row,white_row,white_row,white_row,white_row,white_row,white_row,

                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Blue},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Chocolate},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Cyan},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Fuchsia},

                            white_row,white_row,white_row,white_row,white_row,white_row,white_row,

                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Crimson},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Aquamarine},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.OrangeRed},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.DarkOrchid},

                            white_row,white_row,white_row,white_row,white_row,white_row,white_row,

                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Black},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.DeepSkyBlue},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.DarkGoldenrod},
                            white_col,
                            new Pattern{ WidthPercent = 0.22f, HightPercent = 0.22f, Color = Color.Firebrick},
                        }
                    }
                }

感谢您的宝贵时间,非常感谢您提供任何帮助。

标签: mvvmxamarin.formscalendar

解决方案


因此,经过更多的研究(也许这会对其他人有所帮助),有一个非常好的工具可以重构为一种比自己尝试更容易的方法(尽管我之前不得不使用它几次我猜对了)。使用代码中的示例(或我开始工作的代码),屏幕左侧有一个小画笔(VS2017)帮助我重构。这是我在玩了几分钟后最终生成的代码。现在我确实在代码隐藏中创建了它,但现在我很确定我可以将它重构到我的虚拟机中。

private static SpecialDate TestMethod(DateTime datetime, bool A, bool B, bool C, bool D)
    {
        var AColor = new Color();
        var BColor = new Color();
        var CColor = new Color();
        var DColor = new Color();

        if (A == true) { AColor = Color.Red; } else { AColor = Color.Transparent; };
        if (B == true) { BColor = Color.Yellow; } else { BColor = Color.Transparent; };
        if (C == true) { CColor = Color.Green; } else { CColor = Color.Transparent; };
        if (D == true) { DColor = Color.Blue; } else { DColor = Color.Transparent; };

        var white_row = new Pattern { WidthPercent = .1275f, HightPercent = 0.75f, Color = Color.Transparent };
        var white_col = new Pattern { WidthPercent = 0.04f, HightPercent = 1f, Color = Color.Transparent };
        var aPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = AColor };
        var bPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = BColor };
        var cPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = CColor };
        var dPattern = new Pattern { WidthPercent = 0.22f, HightPercent = 0.22f, Color = DColor };

        return new SpecialDate(datetime)
        {
            BackgroundColor = Color.White,
            TextColor = Color.Black,
            Selectable = true,
            BackgroundPattern = new BackgroundPattern(7)
            {
                Pattern = new List<Pattern>
                        {

                                white_row, white_row, white_row, white_row, white_row, white_row, white_row,
                                    aPattern, white_col, bPattern, white_col, cPattern, white_col, dPattern

                        }
            }
        };
    }

我知道我回答了我自己的问题并将其标记为已回答,但希望它会对其他人有所帮助......展示了如何重构比尝试自己做更简单。


推荐阅读