Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically Create TextBox Issue #940

Closed
turboyang-cn opened this issue Jun 13, 2019 · 6 comments
Closed

Dynamically Create TextBox Issue #940

turboyang-cn opened this issue Jun 13, 2019 · 6 comments

Comments

@turboyang-cn
Copy link

turboyang-cn commented Jun 13, 2019

  • .NET Core Version: 3.0.100-preview6-012264
  • OS: Windows 10
  • OS Version: 17134.471
  • Visual Studio Version: Visual Studio 2019 16.1.3

Errors occurred when I created text boxes dynamically in my code

System.ArgumentException:"Requested value 'Delete' was not found."

TextBox TextBox = new TextBox();

@lindexi
Copy link
Member

lindexi commented Jun 13, 2019

Can you give a minimal repository? I run your code in my project and I can not find the exception.

My version is 3.0.100-preview5-011568

@turboyang-cn
Copy link
Author

https://github.com/turboyang-cn/wpf-core-test

There's no problem with this version. The problem is in the new version (3.0.100-preview6-012264).

@turboyang-cn
Copy link
Author

image

@lindexi
Copy link
Member

lindexi commented Jun 13, 2019

It seems a language issues.

The code will call CommandHelpers.RegisterCommandHandler the register command

CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.Delete , new ExecutedRoutedEventHandler(OnDelete) , onQueryStatusNYI , KeyDelete, SRID.KeyDeleteDisplayString );

In this code, the MS.Internal.Commands.CommandHelpers.RegisterCommandHandler will use SR.Get(srid1) to change the argument srid1 to language value. The SR.Get(srid1) will change the value of srid1 from KeyDelete to the Chinese word 删除 but the expected word is delete

internal static void RegisterCommandHandler(Type controlType, RoutedCommand command, ExecutedRoutedEventHandler executedRoutedEventHandler,
string srid1, string srid2)
{
PrivateRegisterCommandHandler(controlType, command, executedRoutedEventHandler, null,
KeyGesture.CreateFromResourceStrings(SR.Get(srid1), SR.Get(srid2)));
}

The KeyGesture.CreateFromResourceStrings need the arguments with keyGestureToken and the value of keyGestureToken is a Chinese word 删除, not the expected word delete.

And the code will use the Chinese word to ConvertFromInvariantString which will call the PresentationCore.dll!System.Windows.Input.KeyGestureConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object source)

return _keyGestureConverter.ConvertFromInvariantString(keyGestureToken) as KeyGesture;

I find the value of source in KeyGestureConverter.ConvertFrom is the Chinese word.

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source)

And the KeyGestureConverter.ConvertFrom will call Enum.Parse that will throw exception.

And the ConvertFrom will call the keyConverter.ConvertFrom to convert the keyToken and the keyToken is the Chinese word.

object resultkey = keyConverter.ConvertFrom(context, culture, keyToken);

And the KeyConverter will call the KeyConverter.GetKey which the value of fullName is the Chinese word.

object key = GetKey(fullName, CultureInfo.InvariantCulture);

And the KeyConverter.GetKey can not find the keyToken which is the Chinese word and call the Enum to Parse the Key and throw exception.

The Key enum can not find any key name as the Chinese word 删除

default: keyFound = (Key)Enum.Parse(typeof(Key), keyToken, true); break;

@lindexi
Copy link
Member

lindexi commented Jun 13, 2019

This issus can be closed, because @ryalanms fix it in #919

@lindexi
Copy link
Member

lindexi commented Jun 13, 2019

Thank for @ryalanms. And he change SRID.KeyDelete which value is "KeyDelete" to KeyDelete which value is "Delete" that can fix it.

- CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.Delete               , new ExecutedRoutedEventHandler(OnDelete)               , onQueryStatusNYI                  , SRID.KeyDelete,           SRID.KeyDeleteDisplayString                 );
+ CommandHelpers.RegisterCommandHandler(controlType, EditingCommands.Delete               , new ExecutedRoutedEventHandler(OnDelete)               , onQueryStatusNYI                  , KeyDelete,           SRID.KeyDeleteDisplayString                 );

9afa976#diff-8491c698fe3be6a213b6db468e74c815L81

Because ResourceManager.GetString("Delete") will return null and ResourceManager.GetString("KeyDelete") will return the Chinese word "删除" and the SR.GetResourceString will return the resourceKey when not found.

And the Key enum can find the "Delete" value.

@grubioe grubioe closed this as completed Jun 13, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Apr 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants