Windows phone 8.1 color picker control - controls
I was wondering if there was a color picker control for windows phone 8.1 runtime apps that look like this.
Thanks in advance!
My solution is make a ColorPicker class under Colorsource folder (or whatever you want to name it) which contain list of color data
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyApps.Colorsource
{
class ColorPicker
{
public static List<ColorPicker> ColorData()
{
string[] colorNames =
{
"White","Black","Yellow","BananaYellow","LaserLemon","Jasmine","Green","Emerald",
"GreenYellow","Lime","Chartreuse","LimeGreen","SpringGreen","LightGreen",
"MediumSeaGreen","MediumSpringGreen","Olive","SeaGreen","Red","OrangeRed",
"DarkOrange","Orange","ImperialRed","Maroon","Brown","Chocolate",
"Coral","Crimson","DarkSalmon","DeepPink","Firebrick","HotPink",
"IndianRed","LightCoral","LightPink","LightSalmon","Magenta","MediumVioletRed",
"Orchid","PaleVioletRed","Salmon","SandyBrown","Navy","Indigo",
"MidnightBlue","Blue","Purple","BlueViolet","CornflowerBlue","Cyan",
"DarkCyan","DarkSlateBlue","DeepSkyBlue","DodgerBlue","LightBlue","LightSeaGreen",
"LightSkyBlue","LightSteelBlue","Mauve","MediumSlateBlue","RoyalBlue","SlateBlue",
"SlateGray","SteelBlue","Teal","Turquoise","DarkGrey","LightGray"
};
string[] uintColors =
{
"#FFFFFFFF","#FF000000","#FFFFFF00","#FFFFE135","#FFFFFF66","#FFF8DE7E", "#FF008000",#FF008A00","#FFADFF2F","#FF00FF00","#FF7FFF00","#FF32CD32",
"#FF00FF7F","#FF90EE90",
"#FF3CB371","#FF00FA9A","#FF808000","#FF2E8B57","#FFFF0000","#FFFF4500",
"#FFFF8C00","#FFFFA500","#FFED2939","#FF800000","#FFA52A2A","#FFD2691E",
"#FFFF7F50","#FFDC143C","#FFE9967A","#FFFF1493","#FFB22222","#FFFF69B4",
"#FFCD5C5C","#FFF08080","#FFFFB6C1","#FFFFA07A","#FFFF00FF","#FFC71585",
"#FFDA70D6","#FFDB7093","#FFFA8072","#FFF4A460","#FF000080","#FF4B0082",
"#FF191970","#FF0000FF","#FF800080","#FF8A2BE2","#FF6495ED","#FF00FFFF",
"#FF008B8B","#FF483D8B","#FF00BFFF","#FF1E90FF","#FFADD8E6","#FF20B2AA",
"#FF87CEFA","#FFB0C4DE","#FF76608A","#FF7B68EE","#FF4169E1","#FF6A5ACD",
"#FF708090","#FF4682B4","#FF008080","#FF40E0D0","#FFA9A9A9","#FFD3D3D3"
};
// i variable depends on how many color you want to add in my case i have 67 colors
var data = new List<ColorPicker>();
for (int i = 0; i < 68; i++) {
data.Add(new ColorPicker(colorNames[i], uintColors[i]));
}
return data;
}
public ColorPicker(string name, string color)
{
Name = name;
Coloruint = color;
}
public string Name { get; set; }
public string Coloruint { get; set; }
}
}
And then I create a GridView
<GridView x:Name="ColorGrid"
ItemsSource="{Binding}"
VerticalAlignment="Top"
Tapped="ColorGrid_Tapped">
<GridView.ItemTemplate>
<DataTemplate>
<Grid>
<Ellipse Fill="{Binding Coloruint}"
Height="50"
Width="50"
Margin="10"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
OnNavigatedTo on that page add this code
var colorViewModel=ColorPicker.ColorData();
ColorGrid.DataContext = colorViewModel;
To use the color data on gridviewtapped add this code
private void ColorGrid_Tapped(object sender, TappedRoutedEventArgs e)
{
Ellipse senderObject = e.OriginalSource as Ellipse;
if (senderObject != null)
{
//senderObject.Fill;<< This is content color data
}
}
Hope this help :D
i got this idea from
http://spasol.wordpress.com/2013/06/02/custom-color-picker-for-windows-phone/
You can try a custom color picker - here's an article on Nokia Developer Wiki written by Spaso Lazarevic.
It comes down to using a predefined set of colors on a different page, nicely laid out.
Related
.NET MAUI Image wrong size
I'm having a problem when I show an image on .NET MAUI, the size of the image is always bigger than it actually is (blue part in the image below). Screenshot My code is as follows: <Grid> <ScrollView> <VerticalStackLayout> <Image Source="https://cdn-5e5150f5f911c807c41ebdc8.closte.com/wp-content/uploads/IoT-development-kit-article-banner-scaled-900x400.jpg" Aspect="AspectFit" BackgroundColor="Blue"> <Image.Margin> <OnIdiom Phone="10" Tablet="20" Desktop="20"/> </Image.Margin> </Image> </VerticalStackLayout> </ScrollView> </Grid> Is there a way to keep the size of the image in proportion to the actual size?
I made some changes: I tried using data binding in MVVM way. I tried counting image ratio using platform code. The following is my code, For MainPage.xaml, the difference is that i use data binding for image Source and AspectRatio property which would be claimed in MainPageVeiwModel. <Grid> <ScrollView> <VerticalStackLayout> <a:AspectImage Source="{Binding ImageUrl}" AspectRatio="{Binding AspectRatio}" Aspect="AspectFit" BackgroundColor="Blue"> <a:AspectImage.Margin> <OnIdiom Phone="10" Tablet="20" Desktop="20"/> </a:AspectImage.Margin> </a:AspectImage> </VerticalStackLayout> </ScrollView> </Grid> For custom control AspectImage, the difference is that I changed AspectRatio to Bindable property as we use binding for this property. More info Bindable properties. public class AspectImage : Image { public static readonly BindableProperty AspectRatioProperty = BindableProperty.Create("AspectRatio", typeof(double), typeof(AspectRatioContainer), null); public double AspectRatio { get { return (double)GetValue(AspectRatioProperty); } set { SetValue(AspectRatioProperty, value); } } public AspectImage() { SizeChanged += HandleSizeChanged; } private void HandleSizeChanged(object sender, EventArgs e) { if (this.Width > 0 && AspectRatio > 0) { var desiredHeightRequest = this.Width * AspectRatio; if ((int)desiredHeightRequest != (int)HeightRequest) { this.HeightRequest = (int)desiredHeightRequest; InvalidateMeasure(); } } } } For MainPageViewModel, we add AspectRatio and ImageUrl property for custom control and count AspectRatio. public class MainPageViewModel { public string ImageUrl { get; set; } public double AspectRatio { get; set; } public MainPageViewModel() { ImageUrl = "https://cdn-5e5150f5f911c807c41ebdc8.closte.com/wp-content/uploads/IoT-development-kit-article-banner-scaled-900x400.jpg"; AspectRatio = CountAspectRatio(ImageUrl); } private double CountAspectRatio(string imageUrl) { var service = new GetImageSizeService(); Size imageSize = service.GetImageSize(imageUrl); return imageSize.Height / imageSize.Width; } } From above code in MainPageViewModel, we count AspectRatio by call platform code. If you are not familiar with it, i recommend this tutorial first: How To Write Platform-Specific Code in .NET MAUI. To inject platform code in Maui (in Xamarin could use DependencyService): First, in Project folder, create a new partial class, let's call it GetImageSizeService: public partial class GetImageSizeService { public partial Size GetImageSize(string file); } Then creat another partial class in Platforms/iOS folder, called it GetImageSizeService also. Pay attention to the namespace should be the same as above file. public partial class GetImageSizeService { public partial Size GetImageSize(string file) { NSData data = NSData.FromUrl(NSUrl.FromString(file)); UIImage image = UIImage.LoadFromData(data); return new Size((double)image.Size.Width, (double)image.Size.Height); } } Then in MainPageViewModel, we just call this service and count the AspectRatio. =========================== First post============= The link you add did inspire me. And if i understand your question correctly, you could try the following code which worked for me: Create AspectImage custom control which set aspect ratio for width and height public class AspectImage : Image { public double AspectRatio { get; set; } public AspectImage() { SizeChanged += HandleSizeChanged; } private void HandleSizeChanged(object sender, EventArgs e) { if (this.Width > 0 && AspectRatio > 0) { var desiredHeightRequest = this.Width * AspectRatio; if ((int)desiredHeightRequest != (int)HeightRequest) { this.HeightRequest = (int)desiredHeightRequest; InvalidateMeasure(); } } } } For xaml, consume the AspectImage. Here the aspect ratio seems to be 4/9 Approximately equal to 0.44 <Grid> <ScrollView> <VerticalStackLayout> <a:AspectImage Source="https://cdn-5e5150f5f911c807c41ebdc8.closte.com/wp-content/uploads/IoT-development-kit-article-banner-scaled-900x400.jpg" AspectRatio="0.44" Aspect="AspectFit" BackgroundColor="Blue"> <a:AspectImage.Margin> <OnIdiom Phone="10" Tablet="20" Desktop="20"/> </a:AspectImage.Margin> </a:AspectImage> </VerticalStackLayout> </ScrollView> </Grid> Hope it works for you.
Admob ads are not appearing on xamarin forms
I am trying to place a google admob banner ad on a xamarin forms project but I cannot. I have not tried from many sites. When I run the application on both the phone and the emulator, the colorful template of the ad appears at the bottom, but the ad does not. I also tried it with test id. MainPage.xaml <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:App6.CustomRenders;assembly=App6" x:Class="App6.MainPage"> <StackLayout> <!-- Place new controls here --> <Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" /> <local:AdBanner WidthRequest="320" HeightRequest="50"/> </StackLayout> </ContentPage> AdBanner.cs using System; using System.Collections.Generic; using System.Text; using Xamarin.Forms; namespace App6.CustomRenders { public class AdBanner : View { public enum Sizes { Standardbanner, LargeBanner, MediumRectangle, FullBanner, Leaderboard, SmartBannerPortrait } public Sizes Size { get; set; } public AdBanner() { this.BackgroundColor = Color.Accent; } } } enter code here AdBanner_Droid.cs (in Android) using System; using App6; using Android.Gms.Ads; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; using App6.CustomRenders; using App6.Droid.CustomRenders; using Android.Content; [assembly: ExportRenderer(typeof(AdBanner), typeof(AdBanner_Droid))] namespace App6.Droid.CustomRenders { public class AdBanner_Droid : ViewRenderer { Context context; public AdBanner_Droid(Context _context) : base(_context) { context = _context; } protected override void OnElementChanged(ElementChangedEventArgs<View> e) { base.OnElementChanged(e); if (e.OldElement == null) { var adView = new AdView(Context); switch ((Element as AdBanner).Size) { case AdBanner.Sizes.Standardbanner: adView.AdSize = AdSize.Banner; break; case AdBanner.Sizes.LargeBanner: adView.AdSize = AdSize.LargeBanner; break; case AdBanner.Sizes.MediumRectangle: adView.AdSize = AdSize.MediumRectangle; break; case AdBanner.Sizes.FullBanner: adView.AdSize = AdSize.FullBanner; break; case AdBanner.Sizes.Leaderboard: adView.AdSize = AdSize.Leaderboard; break; case AdBanner.Sizes.SmartBannerPortrait: adView.AdSize = AdSize.SmartBanner; break; default: adView.AdSize = AdSize.Banner; break; } // TODO: change this id to your admob id adView.AdUnitId = "ca-app-p"; var requestbuilder = new AdRequest.Builder(); adView.LoadAd(requestbuilder.Build()); SetNativeControl(adView); } } } } looks like this
Problem with Picker items scrolling in windows ToughPad
I am taking one picker like <Picker x:Name="picker" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" ItemDisplayBinding="{Binding ItemName}"/> My Item class is like public class Item { [PrimaryKey, AutoIncrement] public int Id { get; set; } public string ItemName { get; set; } } Now I am adding picker itemssource like List<Item> items= new List<Item>(); items= App.DAUtil.GetDevices(); picker.ItemsSource = items; here I am getting devices list from my local db. When ever I run the code in windows ToughPad the picker items are scrolling continuously but it is working fine when I run the code in Local Machine.Please help me for how to stop continuous scrolling of picker.
Problem with Picker items scrolling in windows ToughPad The matched native control is ComboBox in uwp platform, and loop scroll is by design in touch mode. If you want to tun it off you could refer this case reply then custom Picker renderer. [assembly: ExportRenderer(typeof(Picker), typeof(CustomPickerRenderer))] namespace App35.UWP { public class CustomPickerRenderer : PickerRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Picker> e) { base.OnElementChanged(e); if(Control != null) { Control.ItemsPanel = App.Current.Resources["CustomPanelTemplate"] as ItemsPanelTemplate; } } } } Application.Resources <ResourceDictionary> <ItemsPanelTemplate x:Key="CustomPanelTemplate"> <StackPanel Orientation="Vertical" /> </ItemsPanelTemplate> </ResourceDictionary>
Databinding doesnt display anything
I am trying to print out observable collection into the listview. It is for custom navigation. It should output 2 buttons with icon and one button should have active element. For some reason I am not able to see anything. It works without problem when I use same code in Main.xaml/Main.xaml.cs. Maybe there is limitation on Application class that prevents databinding? App.Xaml <!--test--> <ListView ItemsSource="{Binding NavigationItemss}"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout> <Label Text="{Binding Icon}"></Label> <Label Text="Testing"></Label> </StackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> <!--test--> App.xaml.cs public partial class App : Application { //TODO: Replace with *.azurewebsites.net url after deploying backend to Azure public static string AzureBackendUrl = "http://localhost:5000"; public static bool UseMockDataStore = true; Navigation AppNavigation = new Navigation(); public App() { InitializeComponent(); if (UseMockDataStore) DependencyService.Register<MockDataStore>(); else DependencyService.Register<AzureDataStore>(); this.BindingContext = AppNavigation; MainPage = new NavigationPage(new Main()); } Navigation.cs using HOT_App.Views; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; using System.ComponentModel; using System.Text; using Xamarin.Forms; namespace HOT_App.ViewModels { class Navigation { //public List<NavigationItem> NavigationItems; public ObservableCollection<NavigationItem> NavigationItemss { get; set; } public Navigation() { NavigationItemss = new ObservableCollection<NavigationItem>(); //NavigationItems = new List<NavigationItem>(); NavigationItem Home = new NavigationItem("Home","NavigationHome.png",true); NavigationItem Trends = new NavigationItem("Trends","NavigationTrend.png",false); NavigationItemss.Add(Home); NavigationItemss.Add(Trends); //NavigationItems.Add(Home); //NavigationItems.Add(Trends); } public void ShowActiveNavigation() { System.Diagnostics.Debug.WriteLine(string.Join<NavigationItem>("\n", NavigationItemss)); } public void SetActiveNavigation(string activeNavigationName) { ChangeActiveValue(activeNavigationName); switch (activeNavigationName) { case "Home": //activeNavigation = new NavigationItem("Home"); Application.Current.MainPage.Navigation.PushAsync(new Main(), false); System.Diagnostics.Debug.WriteLine("Home"); break; case "Trends": //activeNavigation = new NavigationItem("Trends"); Application.Current.MainPage.Navigation.PushAsync(new Trends(), false); System.Diagnostics.Debug.WriteLine("Trends"); break; default: //activeNavigation = new NavigationItem("Home"); Application.Current.MainPage.Navigation.PushAsync(new Main(), false); System.Diagnostics.Debug.WriteLine("Home"); break; } } public void ChangeActiveValue(string activeNavigationName) { foreach(NavigationItem navigationItem in NavigationItemss) { if (navigationItem.NavigationItemName == activeNavigationName) { navigationItem.Active = true; } else { navigationItem.Active = false; } } } } }
Normally we don't create pages in App.xaml i think MasterDetailPage is the the fastest way to solve your problem, you just need to configure Master and Detail page,Master page here is your navigation page which you use ListView,and the detail page is your Main and Home pages. you could refer to the MasterDetails Page ps: you could also use Shell if your Xamarin.Forms is 4.0 and above
DataBinding works inside the controlTemplate as well. You can find more info about it here. https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/control-templates/template-binding
How can I set up different footers for TableSections when using a Custom TableView Renderer
I am using a renderer to allow me to set a custom footer in my TableView. The renderer works but I would like to have the capability to set up different footers for the different table sections. For example one footer for table section 0 and another for table section 1, all the way up to table section 5. Here's the XAML that I am using: <!-- <local:ExtFooterTableView x:Name="tableView" Intent="Settings" HasUnevenRows="True">--> <TableView x:Name="tableView" Intent="Settings" HasUnevenRows="True"> <TableSection Title="Cards1"> <ViewCell Height="50"> <Label Text="Hello1" /> </ViewCell> <ViewCell Height="50"> <Label Text="Hello2" /> </ViewCell> </TableSection> <TableSection Title="Cards2"> <TextCell Height="50" Text="Hello"></TextCell> </TableSection> </TableSection> <!-- </local:ExtFooterTableView>--> </TableView> and here is the C# class and renderer: public class ExtFooterTableView : TableView { public ExtFooterTableView() { } } and: using System; using Japanese; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; [assembly: ExportRenderer(typeof(ExtFooterTableView), typeof(Japanese.iOS.ExtFooterTableViewRenderer))] namespace Japanese.iOS { public class ExtFooterTableViewRenderer : TableViewRenderer { protected override void OnElementChanged(ElementChangedEventArgs<TableView> e) { base.OnElementChanged(e); if (Control == null) return; var tableView = Control as UITableView; var formsTableView = Element as TableView; tableView.WeakDelegate = new CustomFooterTableViewModelRenderer(formsTableView); } private class CustomFooterTableViewModelRenderer : TableViewModelRenderer { public CustomFooterTableViewModelRenderer(TableView model) : base(model) { } public override UIView GetViewForFooter(UITableView tableView, nint section) { Debug.WriteLine("xx"); if (section == 0) { return new UILabel() { // Text = TitleForFooter(tableView, section), // or use some other text here Text = "abc", TextAlignment = UITextAlignment.Left // TextAlignment = NSTextAlignment.NSTextAlignmentJustified }; } else { return new UILabel() { // Text = TitleForFooter(tableView, section), // or use some other text here Text = "def", TextAlignment = UITextAlignment.Left // TextAlignment = NSTextAlignment.NSTextAlignmentJustified }; } } } } } The code works but I would like to find out how I can set up a different footer text for different sections in the XAML. Something like this: From what I see it looks like the code is partly there TitleForFooter(tableView, section) but I am not sure how to use it and how I could set it up. Note that I am not really looking for a view model solution. I would be happy to be simply able to specify the section footer text as part of the TableView XAML. I'd appreciate if anyone could give me some advice on this.
First of all, in order to be able to specify the section footer text in XAML - simplest option would be to create a bindable property in TableSection. But as TableSection is sealed, we can't derive it to define our custom bindable properties. So, the next option is to create a attached bindable property. public class Ex { public static readonly BindableProperty FooterTextProperty = BindableProperty.CreateAttached("FooterText", typeof(string), typeof(Ex), defaultValue: default(string)); public static string GetFooterText(BindableObject view) { return (string)view.GetValue(FooterTextProperty); } public static void SetFooterText(BindableObject view, string value) { view.SetValue(FooterTextProperty, value); } } Next step would be to update renderer to retrieve this value for every section: private class CustomFooterTableViewModelRenderer : TableViewModelRenderer { public CustomFooterTableViewModelRenderer(TableView model) : base(model) { } public override UIView GetViewForFooter(UITableView tableView, nint section) { return new UILabel() { Text = TitleForFooter(tableView, section), // or use some other text here Font = UIFont.SystemFontOfSize(14), ShadowColor = Color.White.ToUIColor(), ShadowOffset = new CoreGraphics.CGSize(0, 1), TextColor = Color.DarkGray.ToUIColor(), BackgroundColor = Color.Transparent.ToUIColor(), Opaque = false, TextAlignment = UITextAlignment.Center }; } //Retrieves the footer text for corresponding section through the attached property public override string TitleForFooter(UITableView tableView, nint section) { var tblSection = View.Root[(int)section]; return Ex.GetFooterText(tblSection); } } Sample Usage <local:ExtFooterTableView x:Name="tableView" Intent="Settings" HasUnevenRows="True"> <TableSection Title="Cards1" local:Ex.FooterText="Sample description"> <ViewCell Height="50"> <Label Margin="20,0,20,0" Text="Hello1" /> </ViewCell> <ViewCell Height="50"> <Label Margin="20,0,20,0" Text="Hello2" /> </ViewCell> </TableSection> <TableSection Title="Cards2" local:Ex.FooterText="Disclaimer note"> <TextCell Height="50" Text="Hello"></TextCell> </TableSection> </local:ExtFooterTableView>
It is very simple. you need to add the bindable property for pass value from XAML to CustomRenderer in CustomControl like this: Customer TableView public class ExtFooterTableView : TableView { public ExtFooterTableView() { } } Xaml control code <local:ExtFooterTableView x:Name="tableView" Intent="Settings" HasUnevenRows="True"> Renderer class using System; using UIKit; using Xamarin.Forms; using Xamarin.Forms.Platform.iOS; using yournamespace; using System.ComponentModel; [assembly: ExportRenderer(typeof(ExtFooterTableView), typeof(FooterTableViewRenderer))] namespace yournamespace { public class FooterTableViewRenderer : TableViewRenderer { protected override void OnElementChanged(ElementChangedEventArgs<TableView> e) { base.OnElementChanged(e); } protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { base.OnElementPropertyChanged(sender, e); var view = (ExtFooterTableView)Element; if (e.PropertyName == ExtFooterTableView.IntentProperty.PropertyName) { string intent = view.Intent; // Do your stuff for intent property } if (e.PropertyName == ExtFooterTableView.HasUnevenRowsProperty.PropertyName) { bool hasUnevenRows = view.HasUnevenRows; // Do yout stuff for HasUnevenRow } } } }