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

Fix DataGrid crash in Sample App #4476

Merged
merged 2 commits into from
Feb 4, 2022
Merged

Fix DataGrid crash in Sample App #4476

merged 2 commits into from
Feb 4, 2022

Conversation

HEIGE-PCloud
Copy link
Contributor

Fixes

Fixes #4475

Use CultureInfo.InvariantCulture.DateTimeFormat as IFormatProvider for DateTimeOffset.Parse when loading data source for the DataGrid sample to prevent crashing caused by regional settings.

PR Type

What kind of change does this PR introduce?

Bugfix

What is the current behavior?

The sample crashes immediately when navigated to the DataGrid sample.

What is the new behavior?

Not crash.

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tested code with current supported SDKs
  • New component
    • Pull Request has been submitted to the documentation repository instructions. Link:
    • Added description of major feature to project description for NuGet package (4000 total character limit, so don't push entire description over that)
    • If control, added to Visual Studio Design project
  • Sample in sample app has been added / updated (for bug fixes / features)
  • New major technical changes in the toolkit have or will be added to the Wiki e.g. build changes, source generators, testing infrastructure, sample creation changes, etc...
  • Tests for the changes have been added (for bug fixes / features) (if applicable)
  • Header has been added to all new source files (run build/UpdateHeaders.bat)
  • Contains NO breaking changes

Other information

@ghost
Copy link

ghost commented Feb 3, 2022

Thanks HEIGE-PCloud for opening a Pull Request! The reviewers will test the PR and highlight if there is any conflict or changes required. If the PR is approved we will proceed to merge the pull request 🙌

@ghost ghost requested review from michael-hawker and azchohfi February 3, 2022 09:43
@ghost ghost added bug 🐛 An unexpected issue that highlights incorrect behavior DataGrid 🔠 Issues on DataGrid control sample app 🖼 WinUI 💠 Related to WinUI 3 Version or when paired with External can mean requires fix in WinUI 2/3. labels Feb 3, 2022
@@ -30,6 +31,7 @@ public async Task<IEnumerable<DataGridDataItem>> GetDataAsync()
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri);
IRandomAccessStreamWithContentType randomStream = await file.OpenReadAsync();
_items = new ObservableCollection<DataGridDataItem>();
IFormatProvider provider = CultureInfo.InvariantCulture.DateTimeFormat;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QQ: Was there a particular reason we didn't want to use this inline or was it to just bubble it up to the top of the function here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not quite sure about the cost of calling CultureInfo.InvariantCulture.DateTimeFormat. Consider it is used in a loop, and put it up top seems harmless...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sergio0694 I'm sure could shed some light here, from my understanding it shouldn't make a difference as it should be optimized by the compiler in either case? Definitely curious now though... Sergio any insights you could shed on this scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my benchmark, there is no difference.

using System.Globalization;

string t = "01/01/1900";
const int N = 10000000;
IFormatProvider provider = CultureInfo.InvariantCulture.DateTimeFormat;
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
for (int i = 0; i < N; i ++)
{
    DateTimeOffset.Parse(t, provider);
}
watch.Stop();
Console.WriteLine($"Execution Time (top): {watch.ElapsedMilliseconds} ms");

watch.Reset();
watch.Start();
for (int i = 0; i < N; i ++)
{
    DateTimeOffset.Parse(t, CultureInfo.InvariantCulture.DateTimeFormat);
}
watch.Stop();
Console.WriteLine($"Execution Time (inline): {watch.ElapsedMilliseconds} ms");
Execution Time (top): 3031 ms
Execution Time (inline): 3002 ms
Execution Time (top): 2978 ms
Execution Time (inline): 3010 ms
Execution Time (top): 2991 ms
Execution Time (inline): 3017 ms
Execution Time (top): 2980 ms
Execution Time (inline): 3003 ms
Execution Time (top): 2964 ms
Execution Time (inline): 3031 ms
Execution Time (top): 3038 ms
Execution Time (inline): 3186 ms
Execution Time (top): 2986 ms
Execution Time (inline): 3022 ms
Execution Time (top): 3073 ms
Execution Time (inline): 3014 ms

I'll move it inline then :)

@michael-hawker michael-hawker added this to the 8.0 milestone Feb 4, 2022
@michael-hawker
Copy link
Member

Thanks for fixing this @HEIGE-PCloud! 🦙❤

@michael-hawker michael-hawker merged commit b39d70f into CommunityToolkit:main Feb 4, 2022
@michael-hawker michael-hawker modified the milestones: 8.0, 7.1.3 Jul 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior DataGrid 🔠 Issues on DataGrid control hotfix 🌶 sample app 🖼 WinUI 💠 Related to WinUI 3 Version or when paired with External can mean requires fix in WinUI 2/3.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DataGrid crash in Sample App
3 participants