Mvvm 使用xamforms.controls.calendar生成日历活动
所以,和大多数人一样,我还是个新手,正在浏览c#和Xamarin.Forms的所有内容,但速度异常缓慢。使用XamarinForms和XamForms.Controls.Calendar插件,我试图构建一个日历,该日历可以获取发生的日期和事件的列表,并在底部放置一个小的颜色代码。我回顾了我能找到的所有源代码和主题,并对其进行了重构,构建了下面的代码版本,它符合我的要求,但很难看……我想让它比之前使用的示例和我所做的更具可重用性和MVVM。本质上,我的计划是基于bool值(应该能够在代码中看到)将颜色绑定到活动。所以,挑战是我真的不知道去哪里或做什么,但下面是我认为需要发生的事情。如果这是一个简单的问题,请原谅我,我只是没有看到 XamlPageMvvm 使用xamforms.controls.calendar生成日历活动,mvvm,xamarin.forms,calendar,Mvvm,Xamarin.forms,Calendar,所以,和大多数人一样,我还是个新手,正在浏览c#和Xamarin.Forms的所有内容,但速度异常缓慢。使用XamarinForms和XamForms.Controls.Calendar插件,我试图构建一个日历,该日历可以获取发生的日期和事件的列表,并在底部放置一个小的颜色代码。我回顾了我能找到的所有源代码和主题,并对其进行了重构,构建了下面的代码版本,它符合我的要求,但很难看……我想让它比之前使用的示例和我所做的更具可重用性和MVVM。本质上,我的计划是基于bool值(应该能够在代码中看到)将
<?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);
}
}
}
名称空间MyApp.View.LogPageViews
{
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类日志页:ContentPage
{
公共日志页()
{
初始化组件();
变量日期=新列表();
SetHasNavigationBar(这个,false);
DateTime testdate=新的日期时间(2018,06,26);
//这就是我在尝试执行类似操作时遇到的错误。遇到诸如“无法从转换”和“无法像方法一样使用”之类的错误
//我这里的代码确实有效,但经过重构后就卡住了
添加(新的CalendarActivityVM(testdate,true,true,true,true));
calendar.SelectedDate=(DateTime.Now);
}
}
}
我的日历活动VM(我不知道这是否正确……这是我学习的地方,我认为这是正确的)
名称空间MyApp.ViewModel
{
公共类CalendarActivityVM
{
公共日期时间日期时间{get;set;}
公共bool A{get;set;}
公共布尔B{get;set;}
公共布尔C{get;set;}
公共bool D{get;set;}
公共日历活动VM(DateTime DateTime、bool a、bool b、bool c、bool d)
{
Datetime=Datetime;
A=A;
B=B;
C=C;
D=D;
var AColor=新颜色();
var BColor=新颜色();
var CColor=新颜色();
var DColor=新颜色();
如果(A==true){AColor=Color.Red;}否则{AColor=Color.Transparent;};
如果(B==true){BColor=Color.Yellow;}否则{BColor=Color.Transparent;};
如果(C==true){CColor=Color.Green;}否则{CColor=Color.Transparent;};
如果(D==true){DColor=Color.Blue;}否则{DColor=Color.Transparent;};
var white_row=新图案{WidthPercent=.1275f,HightPercent=0.75f,Color=Color.Transparent};
var white_col=新图案{WidthPercent=0.04f,HightPercent=1f,Color=Color.Transparent};
var aPattern=新图案{WidthPercent=0.22f,HightPercent=0.22f,Color=AColor};
var bPattern=新图案{WidthPercent=0.22f,HightPercent=0.22f,Color=BColor};
var cPattern=新图案{WidthPercent=0.22f,HightPercent=0.22f,Color=CColor};
var dPattern=新图案{WidthPercent=0.22f,HightPercent=0.22f,Color=DColor};
风险值活动=新的背景模式(7)
{
模式=新列表
{
白排,白排,白排,白排,白排,白排,白排,白排,白排,
A模式,白色颜色,B模式,白色颜色,C模式,白色颜色,D模式
}
};
}
}
}
根据示例,正常的“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},
}
}
}
calendar.SpecialDates=新列表{
新的特殊日期(DateTime.Now.AddDays(3))
{
背景颜色=颜色。白色,
TextColor=Color.Black,
可选=真,
背景模式=新背景模式(7)
{
模式=新列表
{
新图案{WidthPercent=0.22f,HightPercent=0.22f,Color=Color.Red,Text=“X”,TextColor=Color.White,TextSize=11,TextAlign=TextAlign.Middle},
白山,
新图案{WidthPercent=0.22f,HightPercent=0.22f,Color=Color.Gold,Text=“Y”,TextColor=Color.White,TextSize=11,TextAlign=TextAlign.Middle},
白山,
新图案{WidthPercent=0.22f,HightPercent=0.22f,Color=Color.Green,Text=“Z”,TextColor=Color.White,TextSize=11,TextAlign=TextAlign.Middle},
白山,
新图案{WidthPercent=0.22f,HightPercent=0.22f,Color=Color.Purple,Text=“Q”,TextColor=Color.White,TextSize=11,TextAlign=TextAlign.Middle},
白排,白排,白排,白排,白排,白排,白排,白排,白排,
新图案{WidthPercent=0.22f,HightPercent=0.22f,Color=Color.Blue},
白山,
新图案{WidthPercent=0.22f,HightPercent=0.22f,Color=Color.Chocolate},
白山,
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},
}
}
}
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
}
}
};
}