On UWP, with a certain combination of views and layouts, there is a circumstance where text in an entry will initially be invisible. This can be remedied in the app by resizing the windows or clicking into the entry. Based on my investigation, there seems to be an issue where the the actual element that holds the text in UWP is not full size.
We also observed a similar but not quite the same issue when there are entries in a List View in a view cell where the text will not appear unless the cursor is brought near the entry. I don't have a repro for this yet but I bring it up because it might be related.
Finally, while I was able to make it appear by setting a height request, the behavior is not quite predictable. In our production apps, setting/not setting height requests was not able to change anything. In our production apps, we fixed it by setting a width request of 5 which for some reason resolved it in our environment.
Repro project below
Text should appear initially upon page load as it does on Android and iOS.
The text is not initially visible in entries unless they are resized or interacted with.
Issue on UWP,
Issue not present on iOS as an example,
8214 might resolve this. I noticed similar when testing for that PR
@bmacombe I'd guess it is (also?) related to #8503, and hopefully fixed in #8536. I don't know if we have been working on the same symptom(?)
Yes I've also noticed this happening since the latest Xamarin Forms version.
@WayaFlyfeather I took the changes from #8536 and made them to #8214 to see if they resolved the issue I was attempting to resolve.
Without 8536, this is how the issue page 2172 looks without applying style changes. The green section is with the 2172 renderer changes and the red is with the unmodified renderer.
With 8536, it seems to cause two text boxes that worked correctly with 8214 to regress and it does fix most of the boxes in the red, but not the auto size editor.
I think it's safe to say there is something strange going on with FormsTextBox that we all haven't exactly nailed down yet. I tried to reproduce the 2172 issues with stock UWP and couldn't
I'm encountering the same issue. Please fix asap.
What is the best workaround?
Same problem here. I can not update to 4.3 until this is fixed.
I noticed this behavior if you change the Text while the Entry is hidden.
Theres also a simmilar issue with switches.
See: #8854
Same issue here. Also looking for a workaround...
I've come up with a workaround. Here you go:
namespace App1
{
public class CustomEditor : Editor
{
public CustomEditor()
{
}
}
}
[assembly: ExportRenderer(typeof(CustomEditor), typeof(UwpEditorRenderer))]
namespace App1.UWP
{
class UwpEditorRenderer : EditorRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
Control.Loaded -= Control_Loaded;
}
if (e.NewElement != null)
{
Control.Loaded += Control_Loaded;
}
}
private void Control_Loaded(object sender, RoutedEventArgs e)
{
// Hack workaround for: https://github.com/xamarin/Xamarin.Forms/issues/8787
Control.Text = string.Empty;
Control.Text = Element.Text;
Control.Loaded -= Control_Loaded;
}
}
}
Thanks @jsiemens for the work around. To make it work for Entry Controls (as the bug describes) I just changed the renderer to an EntryRenderer.
Also needed some additional null checks, ended up with
public class UwpEditorRenderer : EntryRenderer
{
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if (e.OldElement != null && Control != null)
{
Control.Loaded -= Control_Loaded;
}
if (e.NewElement != null && Control != null)
{
Control.Loaded += Control_Loaded;
}
}
private void Control_Loaded(object sender, RoutedEventArgs e)
{
// Hack workaround for: https://github.com/xamarin/Xamarin.Forms/issues/8787
Control.Text = string.Empty;
Control.Text = Element?.Text ?? string.Empty;
Control.Loaded -= Control_Loaded;
}
}
But XF-team, please fix!
I've tried the workaround and it works fine for short strings. But if the string is long and needs to be wrapped, the text is not displayed, and it is only displayed when you resize the window.
Without the workaround, short texts are not displayed, but long texts that need to be wrapped are.
I've updated the workaround so that it also works with strings that have to be wrapped:
protected override void OnElementChanged (ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged (e);
if (Control == null) { return; }
if (e.OldElement != null && Control != null)
{
Control.Loaded -= Control_Loaded;
}
if (e.NewElement != null && Control != null)
{
Control.IsEnabled = true;
Control.Loaded += Control_Loaded;
}
}
private void Control_Loaded (object sender, RoutedEventArgs e)
{
// Hack workaround for: https://github.com/xamarin/Xamarin.Forms/issues/8787
Control.Text = string.Empty;
Control.Text = Element?.Text ?? string.Empty;
Control.Loaded -= Control_Loaded;
}
\
Although Control.IsEnabled
is set to true, the editor on the page is still disabled.
Shouldn't this actually cause the editor to be enabled?
In case it helps anyone, I did find that my originally solution didn't always work. I settled on the following approach which has proven to work 100% of the time as tested. This is specifically for Editor, rather than Entry, so the existing workaround for Entry may be reliable, and this code is specifically looking for a certain named element within the control template so it wouldn't work for Entry although something similar could maybe be done.
private async void Control_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
// Hack workaround for: https://github.com/xamarin/Xamarin.Forms/issues/8787
Control.Loaded -= Control_Loaded;
await Task.Delay(1);
var content = (UIElement) Control.FindName("ContentElement");
Control.Foreground = new SolidColorBrush(Element.TextColor.ToWindowsColor());
}
I have tested my fix in #8536 against this issue, and it appears that it also works for this. So hopefully it will be resolved if/when the PR is approved and merged.
We tested @WayaFlyfeather fix from #8536 in our production code and found that it resolves the issue there as well. We will update if we see any problems but so far it's looking like a good fix.
@jsiemens Your code didn't work for me. What solved it for all my Editors was this:
private async void Control_Loaded(object sender, RoutedEventArgs e)
{
// Hack workaround for: https://github.com/xamarin/Xamarin.Forms/issues/8787
Control.Loaded -= Control_Loaded;
var text = Control.Text;
Control.Text = string.Empty;
await Task.Delay(100);
Control.Text = text;
}
closed by #8536
In case it helps anyone, I did find that my originally solution didn't always work. I settled on the following approach which has proven to work 100% of the time as tested. This is specifically for Editor, rather than Entry, so the existing workaround for Entry may be reliable, and this code is specifically looking for a certain named element within the control template so it wouldn't work for Entry although something similar could maybe be done.
private async void Control_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) { // Hack workaround for: https://github.com/xamarin/Xamarin.Forms/issues/8787 Control.Loaded -= Control_Loaded; await Task.Delay(1); var content = (UIElement) Control.FindName("ContentElement"); Control.Foreground = new SolidColorBrush(Element.TextColor.ToWindowsColor()); }
@jsiemens could you expand a little more on using this for Entrys as well? Also doesn't look like you use the content variable you grabbed.
Hi everyone. In my case the solution with the _delay_ does not work, since in many occasions the application takes longer to load. I propose the following as a solution:
``` C#
protected override void OnElementChanged(ElementChangedEventArgs
{
base.OnElementChanged(e);
if (e.NewElement != null && Control != null)
{
Control.TextChanged += TextChange;
Control.Loaded += ControlLoad;
}
if (e.OldElement != null && Control != null)
{
Control.TextChanged -= TextChange;
Control.Loaded -= ControlLoad;
}
}
private string OriginalText;
private void ControlLoad(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(Element.Text))
{
OriginalText = Regex.Replace(Element.Text, @"\n", "");
Control.Text = string.Empty;
Control.Loaded -= ControlLoad;
}
}
private void TextChange(object sender, Windows.UI.Xaml.Controls.TextChangedEventArgs e)
{
if (!string.IsNullOrEmpty(OriginalText) && !Control.Text.Equals(OriginalText))
{
Control.Text = OriginalText;
Control.TextChanged -= TextChange;
}
}
````
Note that the _regex_ is only necessary in case the string contains the character '\n'
@jalbertSyncroTech It's not closed ;)
@jalbertSyncroTech It's not closed ;)
Sorry, I saw the red "closed" icon and thought that was it. Feel free to delete this comment.
I am slightly confused as to which is the best workaround for this issue. I have tried each of the suggested workarounds and none work consistently. Would someone be able to share a workaround that works for Entry controls only?
@jsiemens Your code didn't work for me. What solved it for all my Editors was this:
private async void Control_Loaded(object sender, RoutedEventArgs e) { // Hack workaround for: https://github.com/xamarin/Xamarin.Forms/issues/8787 Control.Loaded -= Control_Loaded; var text = Control.Text; Control.Text = string.Empty; await Task.Delay(100); Control.Text = text; }
After further testing, it seems that if I use the workaround above but use a slightly increased delay (150 rather 100), the workaround is more consistent.
I can confirm that none of the solutions above work 100% of the time, however, increasing the delay to 150 or 200 does seem to be working most of the time 95%.
This is not a very good solution though as I think it would depend on how much processing is going on at the time and the speed of the computer. Slower computers or apps with a lot of views loading at the same time may require a higher delay.
same problem
I can confirm that none of the solutions above work 100% of the time, however, increasing the delay to 150 or 200 does seem to be working most of the time 95%.
This is not a very good solution though as I think it would depend on how much processing is going on at the time and the speed of the computer. Slower computers or apps with a lot of views loading at the same time may require a higher delay.
@Russc0 I am experiencing the same behaviour that you have reported, works 95% of the time.
@pauldiston not working all the time ... If I use breakpoints and navigate code, it works fine. I increased the delay but seems that control does not have text inside.
I used like:
private async void Control_Loaded(object sender, RoutedEventArgs e)
{
if (Control == null)
{
return;
}
Control.Loaded -= Control_Loaded;
// Hack workaround for: https://github.com/xamarin/Xamarin.Forms/issues/8787
Control.Text = string.Empty;
await Task.Delay(150);
Control.Text = Element.Text ?? string.Empty;
Control.Height = Element.HeightRequest;
}
Not all the time works... Common Xamarin teams, please fix it.
@hartez Moving the discussion I started to this issue related to the text visibility/height to avoid polluting that collection view issue more.
I have been investigating the reverted PR #8536 to see if I can figure out why it fixed some things and then broke the previous hight fix PR.
Ran across this in the Microsoft UI XAML repro, the discussion about off tree measurement might be relevant.
@PureWeen it is a possibility to merge changes in 4.7 ?
Hey @samhouts I think this issue was closed by accident, #11140 did fix this until #11350 was merged, hopefully, #11351 will fix if that solution is approved.
Yep, GitHub got a little excited. Thanks, @bmacombe !
This bug prevents me to update my project for nearly six month. It would be really a pleasure if it could be solved in the next service release. Thank you.
@sude22 There is a PR open to address this #11351 in the review process.
@sude22 i think it fixed in 4.8 pre1 because my editor finally works fine
With V4.7.0.1142+463-sha.333e71c12-azdo.3888487 I still have the problem.
@sude22 and everyone else on this issue, Thanks to some help from @PureWeen #11351 was merged into 4.7 today. Hopefully, we'll see it in the next 4.7 SR
Do we think this will be in the next 4.7.0 SR or shal I implement the workaround from here: https://github.com/xamarin/Xamarin.Forms/issues/8787#issuecomment-579829064
For me, this issue seems to be fixed in 4.7.0.1179.
closed by #11351
I'm seeing this problem again in v4.8.0.1269, had to re-activate the workaround above to get the values to show on UWP.
For me v4.8.0.1269 is the first release to fix the problem.
For me v4.8.0.1269 is the first release to fix the problem.
Hmm. I had a form with several Entry boxes (IsEnabled="false" on them to make them read only, if that matters). I set their bound property in the VM and on iOS, the values appear. On UWP, they did not appear. I turned on the fix above with the EntryRenderer (we'd implemented it when we saw this problem before, but had since disabled it) and the values started appearing on UWP as they were on iOS.
@cmpalmer66 Can you create a repro project? I just pulled the 4.8 branch and tested all the test pages and everything still looks good.
If you can, probably best to create a new issue also since this one was closed.
I will try. Maybe it's a coincidence or glitch, but I'll see what I can do and create a new issue if I can duplicate.
If you do, ping me on it and I'll try to take a look. I want to squash all these UWP not showing until resizing issues, they are driving me nuts too!
Should this issue be reopened or is there a build of XF that has fixed this issue?
Guys, this is the solution that works for me 100% but none of the above, my XF version 4.7.0.1351
After 50ms await just update the layout.
Do not need to reset the text as the main problem of the inner height of the TextBox
private async void Control_LoadedOnElementChanged(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(Element.Text) && Control != null)
{
// HACK: Entry text initially invisible. See Url: https://github.com/xamarin/Xamarin.Forms/issues/8787
await Task.Delay(50);
Control.UpdateLayout();
Control.Loaded -= Control_LoadedOnElementChanged;
}
}
Most helpful comment
@pauldiston not working all the time ... If I use breakpoints and navigate code, it works fine. I increased the delay but seems that control does not have text inside.
I used like:
Not all the time works... Common Xamarin teams, please fix it.