Is there some way to see the logs of the Xamarin.Forms front-end?
I am somewhat familiar with Blazor, and typically you can see front-end errors by opening the Developer Tools => Console in the browser. There you will see errors and warnings, that typically would not manifest themselves in the debugging environment within C# or VS (as far as I am aware).
I am looking for an equivalent in Xamarin.Forms.
A specific example of where I would want this, is exceptions related to binding, in the view's .xaml file.
In my CategoryPage.xaml file, I am binding a hex color code, to the variable ColorCode:
<StackLayout BackgroundColor="{Binding ColorCode}" Padding="20">
Assuming everything else is working correctly, if I have an incorrect color hex value for the ColorCode variable I am binding, the BackgroundColor would default to white.
Looking at the symptomatic manifestation of the problem (a white BackgroundColor instead of whatever other color I expect), it may be hard to know the root cause.
Therefore, I'm looking for some log, that displays front-end exceptions, along the lines of "Error: Could not parse hex value 9C27B0", in the scenario I have described.
This would indicate to me that the binding does occur, but that I have an incorrect value in my repository. Instead of the front-end failing silently.
To begin with, Hex color starts with a pound sign or hashtag (#) and are followed by six letters (A-F) and/or number (digit from 0-9). Alphabetic characters may be uppercase or lowercase. So the ColorCode should follow this rule to make it valid.
Furthermore, you can use Regex to check whether a given string is a valid Hex code or not and then use System.Diagnostics.Debug.WriteLine(); to display it in the VS Output panel. I used a invalid data:#EFFA07A and it can be shown in the VS Output panel like below:
Here's the code below for your reference:
public partial class MainPage : ContentPage
{
private string _color;
public Color ColorCode
{
get => Color.FromHex(_color);
set
{
if (!(Regex.Match(_color, "^#[0-9a-fA-F]{6}$").Success))
{
System.Diagnostics.Debug.WriteLine("Error: Could not parse hex value: ");
}
else
{
if (Color.FromHex(_color) == value) return;
var a = Color.FromHex(_color);
a = value;
OnPropertyChanged(nameof(ColorCode));
}
}
}
public MainPage()
{
InitializeComponent();
ColorCode = Color.FromHex(_color = "#EFFA07A");
BindingContext = this;
}
}
Related
I have a WinForms application, in which I have a label, where I bind a decimal to. I have also added a .Format method to the binding. Like this:
// Add data binding for the lavel
Binding bindWithFormat = new Binding("Text", viewModel, nameof(viewModel.BindingNumber));
bindWithFormat .Format += viewModel.FormatAsNumber;
lblNumber.DataBindings.Add(bindWithFormat);
// Formatting function
public void FormatAsNumber(object sender, ConvertEventArgs e)
{
// The method converts only to string type. Test this using the DesiredType.
if (e.DesiredType != typeof(string)) return;
// Formats the value with thousand separator and zero decimals
e.Value = String.Format("{0:N0}", e.Value);
}
This works fine under normal circumstances, but if I choose a particular type of Digit Grouping Symbol, it looks like this (it is supposed to show "15 000 000"):
I first thought it was when I used a blank space (" ") as symbol, but when I explicitly type a blank space, then it shows it as intended. However, there is another symbol I can chose in the regional settings, which looks like a space, but it causes the above formatting error when selected (unlike when I explicitly type space):
What the heck is going on? According to this website, the symbol is "no break space" (U+00A0). So it is a space. But not a space. And for some reason, it seriously messes up the formatting. What to do?
Bonus info: After playing around some more, it seems to only affect that specific font, that I was using (it only exists in my company). If I change fonts to e.g. Segoe UI, then the problem disappears.
In Visual Studio 2013 when I encapsulate a field starting with i letter. It generates a property with starting letter I
int inside = 0;
public int Inside
{
get { return inside; }
set { inside = value; }
}
But in Visual Studio 2015 if I encapsulate the same field it generates a property starting with İ letter. Applying some indentation rules and adding a space between get and set modifiers
int inside = 0;
public int İnside
{
get
{
return inside;
}
set
{
inside = value;
}
}
How can I change this behaviour? I am using a Turkish keyboard if it is relevant and system culture is Turkish
You typed the Turkish i by mistake, instead of the English i.
It seems this is a debug of the refactoring code, which doesn't handle the user's culture in a safe way when converting the first letter to uppercase.
The Turkish i is one of the most common examples used to demonstrate the differences between cultures, as I is the upper case of `ý. Check for example the Writing Culture-Safe Managed Code article in MSDN.
Make sure you've switched your keyboard to English when you want to type Latin variable names.
As the OP commented, the outcome isn't affected by the keyboard language but the user's locale. This means that the Roslyn refactoring responsible for encapsulating fields perform a ToUpper call using the current CultureInfo instead of the invariant one.
Roslyn refactorings are available as open source so it's easy to find the relevant code. In AbstractEncapsulateFieldService.cs, the GeneratePropertyName method makes a call to char.ToUpper without specifying a culture:
return char.ToUpper(baseName[0]).ToString() + baseName.Substring(1);
It should be easy to change this to :
return char.ToUpper(baseName[0],CultureInfo.InvariantCulture).ToString()
+ baseName.Substring(1);
UPDATE
I opened an issue on the Roslyn site.
UPDATE 2
Unfortunately, the issue was closed as "By Design". It seems that whoever closed it confused the user's locale settings with the language used when writing text
I'm trying to show currency symbols on the dropdown in my monodroid application.
As you know currency units contain some thing like "र". but when I run application, the drop down just show a rectangle instead of "र".
How I can make it human-readable?
EDIT
Actually I parse this json for accessing to the unit ( saving the name attribute to a string variable):
{"id":"167","name":"\u0930","type":"4","enabled":"1","tosi":"0.0182","index":"1","extra":"INR","extra2":"Indian Rupee","extra3":"India","extra4":"Paisa","seperator":",","d_seperator":"","after_before":"0"},
When I parse it, in run-time the string variable includes "र" but when I show it on the dropdown the device show a box.
So according to 'Sam' comment I use this code. I pass the string varible to method and show the return string to the dropdow. but yet I see a box :(
public static string ConvertUnitsEncoding(Activity act,string Encoded){
try {
if( Encoded =="र")
return act. Resources .GetString(Resource .String .IndianUnit );
else
return Encoded ;
} catch (Exception ex) {
RltLog .HandleException (ex);
return Encoded ;
}
}
You've got two options:
Either load a custom font that includes that special character:
TextView tv = (TextView)findViewById(R.id.tv);
// Put the font in the asset folder
Typeface tf = Typeface.createFromAsset(Context.Assets, "Symbol.ttf");
tv.setTypeface(tf);
Most of the installed fonts on Windows have a currency subset which includes currency symbols but not Rupee. I read somewhere that Microsoft Update will add the Rupee to the fonts but I don't have it on my system. I have found Amty Currency Font with Rupee support but I'm not sure how useful it would be for your case. Try it.
Or simply use a small image for that purpose. I would prefer this approach because it's platform independent and you can find lots of symbol icons out there. Something like this:
How do I change the default newline character from Windows \r\n to Unix style \n? Ideally I change the Environment somehow and have it done globally so I don't need to modify a bunch of string/textwriter code.
Unfortunately this is not really doable. The primary way by which developers embed new lines in their application is by using Enviornment.NewLine. This is a static property which returns Window style new lines
public static string NewLine {
get {
return "\r\n";
}
}
There is no way to modify the return of this property from your code.
Your best option for changing new line endings is to develop a library which converts known sources of embedding Windows new lines to unix ones. For example a stream and string converter would likely be in order.
In my c# program I have a very simple DevExpress edit box that represents a numeric value.
What I would like to do is to add a restriction on the number of decimals in such a way that:
Users cannot type, paste or in any other way enter a value that contains more than a predefined number of decimals. I fact I just want to editbox to ignore the user's typing as soon as 3 decimals have been entered.
If a programmer sets the edit box' text, the value is rounded so that it meets the requirements.
What is the best way to do this?
Ps.: I thought this would solve my problem:
valueTextEdit.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
valueTextEdit.Properties.DisplayFormat.FormatString = "#.000;[#.000];0.000";
But it doesn't seem to do anything. I can still enter values with 10 decimals. Also in code I can set the edit box text to a value with a larger number of decimals.
You need to set the UseMaskAsDisplayFormat = true:
Have a look here and here
EDIT:
Nice example here
try this
MaskTextBox control of .Net
you can use MaskedTextBox for entering data in numeric format in text box.
i hope this will help you
thank you
You can build your own class that derives (inherits) from TextBox, then override property Text to add your requirments:
internal class SmartTextBox : TextBox
{
public SmartTextBox()
{
}
public override string Text
{
get
{
return base.Text;
}
set
{
// validate 'value' before setting it
base.Text = value;
}
}
}
After you build your project, you will find your new Control called SmartTextBox with .NET Controls.
EDIT: However, if you use TextBox for numeric input only, why don't you use NumericUpDown Control? It is much better, users can not input characters and you can even set the precision of decimal number.