I want to be able to set a property of a MudBlazor Date Picker component based on breakpoint. I can only think of having 2 versions and hiding one or the other but I am hoping there is a cleaner way.
And more generally is there an easy way to detect in the #code section what breakpoint the user is in?
You can use IBreakpointService
here is an example of changing the DateTime picker value by changing the breakpoint.
https://try.mudblazor.com/snippet/GkwcklvshoBJfwUS
#using MudBlazor.Services
<MudCard Class="pa-5">
<MudDatePicker #bind-Date="_dateTime"/>
</MudCard>
#code {
private DateTime? _dateTime { get; set; } = DateTime.Now;
[Inject] IBreakpointService BreakpointListener { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await BreakpointListener.Subscribe(breakpoint =>
{
switch (breakpoint)
{
case Breakpoint.Xs:
_dateTime = DateTime.Now.AddDays(1);
break;
case Breakpoint.Sm:
_dateTime = DateTime.Now.AddDays(2);
break;
case Breakpoint.Md:
_dateTime = DateTime.Now.AddDays(3);
break;
case Breakpoint.Lg:
_dateTime = DateTime.Now.AddDays(4);
break;
case Breakpoint.Xl:
_dateTime = DateTime.Now.AddDays(5);
break;
case Breakpoint.Xxl:
_dateTime = DateTime.Now.AddDays(6);
break;
case Breakpoint.SmAndDown:
_dateTime = DateTime.Now.AddDays(7);
break;
case Breakpoint.MdAndDown:
_dateTime = DateTime.Now.AddDays(8);
break;
case Breakpoint.LgAndDown:
_dateTime = DateTime.Now.AddDays(9);
break;
case Breakpoint.XlAndDown:
_dateTime = DateTime.Now.AddDays(10);
break;
case Breakpoint.SmAndUp:
_dateTime = DateTime.Now.AddDays(11);
break;
case Breakpoint.MdAndUp:
_dateTime = DateTime.Now.AddDays(12);
break;
case Breakpoint.LgAndUp:
_dateTime = DateTime.Now.AddDays(13);
break;
case Breakpoint.XlAndUp:
_dateTime = DateTime.Now.AddDays(14);
break;
case Breakpoint.None:
_dateTime = DateTime.Now.AddDays(15);
break;
case Breakpoint.Always:
_dateTime = DateTime.Now.AddDays(16);
break;
}
InvokeAsync(StateHasChanged);
});
StateHasChanged();
}
await base.OnAfterRenderAsync(firstRender);
}
}
Related
I have the below requirement.
I need to perform the sum of each field across multiple records of the same entity However while performing the sum, I also need to check the type and cast them accrodingly. For eg, For whole number cast to Int, For Decimal cast to decimal. Also some of the values are aliased value too. I am looking for a generic function which I can call for both alias fields and direct fields and it will return me the value based on the type
Background on the code written below -
Attribute List is the list of all attributes that belong to the
entity.
Format in which the field values are stored in AttributeList-
AttributeList = { "price ", "quantity", "contact.revenue", "opportunity.sales"}
price, quantity - fields of main entity on which we are querying
contact.revenue, opportunity.sales - fields of the aliased entities,
entity name is appended to understand which entity's field it is
Below is the code which i have tried so far -
I only have decimal and whole number fields in my attributeList.
private void calculate(List<string> attributeList,List<Entity> mainEntityList,Guid targetId,Guid oppId,Guid contactId)
{
var mainentity = new mainEntity();
mainentity.Id = targetId;
var opportunity = new Opportunity();
opportunity.Id = oppId;
var contact = new Contact();
contact.Id = contactId;
foreach (var attribute in attributeList)
{
var fieldSum = new decimal(0);
int intFieldSum = 0;
bool attributeFound = false;
foreach (var entity in mainEntityList)
{
if (entity.Contains(attribute))
{
var type = entity[attribute].GetType().Name;
attributeFound = true;
switch (type)
{
case "AliasedValue":
var aliasedFieldValue = entity.GetAttributeValue<AliasedValue>(attribute);
if (aliasedFieldValue.Value.GetType().Name == "Decimal")
{
decimalFieldSum += (decimal)aliasedFieldValue.Value;
}
else
{
intFieldSum += (int)aliasedFieldValue.Value;
}
break;
case "Decimal":
decimalFieldSum += entity.GetAttributeValue<decimal>(attribute);
break;
case "Int32":
intFieldSum += entity.GetAttributeValue<int>(attribute);
break;
default:
break;
}
}
}
if (attributeFound)
{
if (attribute.Contains("opportunity"))
{
opportunity[attribute] = decimalFieldSum != 0 ? decimalFieldSum : intFieldSum;
}
else if (attribute.Contains("contact"))
{
contact[attribute] = decimalFieldSum != 0 ? decimalFieldSum : intFieldSum;
}
else
{
mainentity[attribute] = decimalFieldSum != 0 ? decimalFieldSum : intFieldSum;
}
}
}
service.update(opportunity);
service.update(contact);
service.update(mainentity);
}
Any help would be appreciated.
Just a little bit edited your code.
...
var fieldSum = new decimal(0);
foreach (var entity in mainEntityList)
{
fieldSum += GetAttrValue(entity, attribute);
}
...
You can use this function to calculate fieldSum variable which is of decimal type.
private decimal GetAttrValue(Entity entity, string attribute)
{
var attrValue = new decimal(0);
if (!entity.Contains(attribute) || entity.Attributes[attribute] == null)
{
return attrValue;
}
var type = entity.Attributes[attribute].GetType().Name;
switch (type)
{
case "AliasedValue":
var aliasedFieldValue = entity.GetAttributeValue<AliasedValue>(attribute);
attrValue = type == "Decimal" ? (decimal)aliasedFieldValue.Value : (int)aliasedFieldValue.Value;
break;
case "Decimal":
attrValue = entity.GetAttributeValue<decimal>(attribute);
break;
case "Int32":
attrValue = entity.GetAttributeValue<int>(attribute);
break;
default:
break;
}
return attrValue;
}
On the other hand if you just need a generic function which will return decimal or int value for an attribute you can use this
private T GetAttrValue<T>(Entity entity, string attribute)
{
if (!entity.Contains(attribute) || entity.Attributes[attribute] == null)
{
return default(T);
}
T result;
var type = entity.Attributes[attribute].GetType().Name;
if (type == "AliasedValue")
{
var aliasedFieldValue = entity.GetAttributeValue<AliasedValue>(attribute);
result = (T)aliasedFieldValue.Value;
}
else
{
result = entity.GetAttributeValue<T>(attribute);
}
return result;
}
--Update--
So, here is the whole code if I understand you requirements right.
First of all add this class.
public class AttributeInfo
{
public string Name { get; set; }
public Type Type { get; set; }
public decimal DecimalSum { get; set; } = new decimal(0);
public int IntSum { get; set; } = 0;
}
And add this function
private void SetValue(Entity entity, AttributeInfo attributeInfo)
{
if (entity.Contains(attributeInfo.Name))
{
switch (attributeInfo.Type.Name)
{
case "Decimal":
entity[attributeInfo.Name] = attributeInfo.DecimalSum;
break;
case "Int32":
entity[attributeInfo.Name] = attributeInfo.IntSum;
break;
default:
break;
}
}
}
Then this is you Calculate function
private void Calculate(List<string> attributeList, List<Entity> mainEntityList, Guid targetId, Guid oppId, Guid contactId)
{
var mainentity = new mainEntity();
mainentity.Id = targetId;
var opportunity = new Opportunity();
opportunity.Id = oppId;
var contact = new Contact();
contact.Id = contactId;
var attributesInfo = new List<AttributeInfo>();
foreach (var attribute in attributeList)
{
var attributeInfo = new AttributeInfo
{
Name = attribute
};
foreach (var entity in mainEntityList)
{
if (entity.Contains(attribute))
{
attributeInfo.Type = entity[attribute].GetType();
switch (attributeInfo.Type.Name)
{
case "AliasedValue":
var aliasedFieldValue = entity.GetAttributeValue<AliasedValue>(attribute);
if (aliasedFieldValue.Value.GetType().Name == "Decimal")
{
attributeInfo.DecimalSum += (decimal)aliasedFieldValue.Value;
}
else
{
attributeInfo.IntSum += (int)aliasedFieldValue.Value;
}
break;
case "Decimal":
attributeInfo.DecimalSum += entity.GetAttributeValue<decimal>(attribute);
break;
case "Int32":
attributeInfo.IntSum += entity.GetAttributeValue<int>(attribute);
break;
default:
break;
}
}
}
attributesInfo.Add(attributeInfo);
}
foreach (var attributeInfo in attributesInfo)
{
if (attributeInfo.Type != null)
{
SetValue(mainentity, attributeInfo);
SetValue(opportunity, attributeInfo);
SetValue(contact, attributeInfo);
}
}
service.update(mainentity);
service.update(opportunity);
service.update(contact);
}
I should say that the structure of the calculate function still seems weird for me. However, here I tried to keep the main structure.
i have below question on page creation, i received nothing after the break. Please help.
private void ItemAction(DashboardMultipleTileItemData item)
{
switch (item.Title)
{
case "Contacts":
new NavigationPage(new TabControlAndroidSamplePage());
break;
case "Documents":
Application.Current.MainPage.Navigation.PushAsync(new EmployeePerformanceDashboardPage());
break;
//case "Enquiry":
// Application.Current.MainPage.Navigation.PushAsync(new WebSitePage()));
// break;
}
}
The Solution will be:-
private void ItemAction(DashboardMultipleTileItemData item)
{
switch (item.Title)
{
case "Contacts":
Application.Current.MainPage = new NavigationPage(new TabControlAndroidSamplePage());
break;
case "Documents":
Application.Current.MainPage.Navigation.PushAsync(new EmployeePerformanceDashboardPage());
break;
//case "Enquiry":
// Application.Current.MainPage.Navigation.PushAsync(new WebSitePage()));
// break;
}
}
i'm trying to implement a NSTableView in my project and fill it with specific data. This works quite fine. But now, i want to be able, to hide some columns, color specific cells, or color specific rows. I made something similar in java, but i really don't know to to do this in Xamarin:Mac.
Here is the code for my delegate:
public class Mp3FileTableDelegate : NSTableViewDelegate {
private const string CellIdentifier = "FileCell";
private Mp3FileDataSource DataSource;
public Mp3FileTableDelegate (Mp3FileDataSource datasource) {
this.DataSource = datasource;
}
public override NSView GetViewForItem (NSTableView tableView, NSTableColumn tableColumn, nint row) {
// This pattern allows you reuse existing views when they are no-longer in use.
// If the returned view is null, you instance up a new view
// If a non-null view is returned, you modify it enough to reflect the new data
NSTextField view = (NSTextField)tableView.MakeView (CellIdentifier, this);
if (view == null) {
view = new NSTextField ();
view.Identifier = CellIdentifier;
view.BackgroundColor = NSColor.Clear;
view.Bordered = false;
view.Selectable = false;
view.Editable = true;
view.EditingEnded += (sender, e) => {
SetNewValueInMp3File (DataSource.AudioFiles [(int)row], tableColumn, view.StringValue);
};
}
AudioFile audioFile = DataSource.AudioFiles [(int)row];
// Setup view based on the column selected
switch (tableColumn.Title) {
case "Path":
view.StringValue = audioFile.getPathWithFilename ();
break;
}
if (audioFile.GetType () == typeof(Mp3File)) {
Mp3File mp3File = (Mp3File)audioFile;
switch (tableColumn.Title) {
case "Artist":
view.StringValue = mp3File.Artist;
break;
case "Title":
view.StringValue = mp3File.Title;
break;
case "Album":
view.StringValue = mp3File.Album;
break;
case "BPM":
view.StringValue = mp3File.BPM;
break;
case "Comment":
view.StringValue = mp3File.Comment;
break;
case "Year":
view.StringValue = mp3File.Year;
break;
case "Key":
view.StringValue = mp3File.InitialKey;
break;
case "Quality":
view.StringValue = mp3File.Album;
break;
case "Length":
view.StringValue = mp3File.Album;
break;
}
}
return view;
}
private void SetNewValueInMp3File (AudioFile file, NSTableColumn tableColumn, String value) {
if (file.GetType () == typeof(Mp3File)) {
Mp3File mp3File = (Mp3File)file;
switch (tableColumn.Title) {
case "Artist":
mp3File.Artist = value;
break;
case "Title":
mp3File.Title = value;
break;
case "Album":
mp3File.Album = value;
break;
case "BPM":
mp3File.BPM = value;
break;
case "Comment":
mp3File.Comment = value;
break;
case "Year":
mp3File.Year = value;
break;
case "Key":
mp3File.InitialKey = value;
break;
}
}
}
}
And here for my datasource:
public class Mp3FileDataSource : NSTableViewDataSource {
public List<AudioFile> AudioFiles = new List<AudioFile> ();
public Mp3FileDataSource () {
}
public override nint GetRowCount (NSTableView tableView) {
return AudioFiles.Count;
}
}
I would be very thankful, if anyone could help me a little.
Thanks
I have an MVC3 application and I want to pass my model to a method which builds a command object. The reason being that I have lots of methods with command objects and I want the code to be better written.
private static SqlCommand CommandObj(vw_UserManager_Model model)
{
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
foreach (var item in model)
{
switch (property.PropertyType.Name)
{
case "String":
command.Parameters.Add("#" + property.Name, SqlDbType.VarChar).SqlValue = property;
break;
case "Guid":
command.Parameters.Add("#" + property.Name, SqlDbType.UniqueIdentifier).SqlValue = property;
break;
case "Int32":
command.Parameters.Add("#" + property.Name, SqlDbType.Int).SqlValue = property;
break;
case "Boolean":
//switch (property.Name.FirstOrDefault())
//{
// case true:
// command.Parameters.Add("#isactive", SqlDbType.Bit).SqlValue = 1;
// command.Parameters.Add("#isapproved", SqlDbType.Bit).SqlValue = 1;
// break;
// case false:
// command.Parameters.Add("#isactive", SqlDbType.Bit).SqlValue = 0;
// command.Parameters.Add("#isapproved", SqlDbType.Bit).SqlValue = 0;
// break;
//}
break;
}
}
return command;
}
Currently this code won't compile because I can't enumerate through my model like this. What I want to do is loop through each item in the model and do a switch statement to build the correct dbType parameter.
Anyone have suggestions for how to change this code?
Thanks!!
Hopefully I understood your question. Seems like you may be trying to do something like this. Here's my model class:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public bool Married { get; set; }
}
Here's the code that loops through the model properties:
public static void Main(string[] args)
{
Person person = new Person();
var modelProperties = person.GetType().GetProperties();
foreach (var property in modelProperties)
{
switch (property.PropertyType.Name)
{
case "String":
Console.WriteLine("Property {0} is a string", property.Name);
break;
case "Int32":
Console.WriteLine("Property {0} is an int", property.Name);
break;
case "Boolean":
Console.WriteLine("Property {0} is a boolean", property.Name);
break;
default:
Console.WriteLine("Type unknown!");
break;
}
}
Hope this helps.
I have gone through several Q&A here and on different portals but cannot get this working...
My page orientation is Portrait...
<Rectangle x:Name="videoRectangle" Margin="0,0,0,0">
<Rectangle.Fill>
<VideoBrush x:Name="viewfinderBrush" AlignmentX="Left" AlignmentY="Top" Stretch="UniformToFill">
<VideoBrush.RelativeTransform>
<CompositeTransform x:Name="videoBrushTransform" CenterX="0.5" CenterY="0.5" />
</VideoBrush.RelativeTransform>
</VideoBrush>
</Rectangle.Fill>
</Rectangle>
the code behind ARPage_OrientationChanged never gets invoked
public ARPage()
{
InitializeComponent();
this.OrientationChanged += ARPage_OrientationChanged;
}
void ARPage_OrientationChanged(object sender, OrientationChangedEventArgs e)
{
abc.Text = e.Orientation.ToString();
switch (e.Orientation)
{
case PageOrientation.Landscape:
case PageOrientation.LandscapeLeft:
videoBrushTransform.Rotation = 0;
break;
case PageOrientation.LandscapeRight:
videoBrushTransform.Rotation = -45;
break;
case PageOrientation.Portrait:
case PageOrientation.PortraitUp:
videoBrushTransform.Rotation = -270;
break;
case PageOrientation.PortraitDown:
videoBrushTransform.Rotation = -90;
break;
}
}
The video brush always show video in landscape mode...
what i am doing wrong
it is not necessary to assign the event in the constructor, you can do this:
public ARPage()
{
InitializeComponent();
}
protected override void OnOrientationChanged(OrientationChangedEventArgs e)
{
abc.Text = e.Orientation.ToString();
switch (e.Orientation)
{
case PageOrientation.Landscape:
case PageOrientation.LandscapeLeft:
videoBrushTransform.Rotation = 0;
break;
case PageOrientation.LandscapeRight:
videoBrushTransform.Rotation = -45;
break;
case PageOrientation.Portrait:
case PageOrientation.PortraitUp:
videoBrushTransform.Rotation = -270;
break;
case PageOrientation.PortraitDown:
videoBrushTransform.Rotation = -90;
break;
}
base.OnOrientationChanged(e);
}