コード例 #1
0
        private void BuildEncodingIndicatorFlyout()
        {
            if (StatusBar == null) return;

            if (EncodingSelectionFlyout.Items?.Count > 0)
            {
                return;
            }

            var reopenWithEncoding = new MenuFlyoutSubItem()
            {
                Text = _resourceLoader.GetString("TextEditor_EncodingIndicator_FlyoutItem_ReopenWithEncoding"),
                FlowDirection = FlowDirection.RightToLeft,
                Name = "ReopenWithEncoding"
            };

            var saveWithEncoding = new MenuFlyoutSubItem()
            {
                Text = _resourceLoader.GetString("TextEditor_EncodingIndicator_FlyoutItem_SaveWithEncoding"),
                FlowDirection = FlowDirection.RightToLeft,
                Name = "SaveWithEncoding"
            };

            // Add auto guess Encoding option in ReopenWithEncoding menu
            reopenWithEncoding.Items?.Add(CreateAutoGuessEncodingItem());
            reopenWithEncoding.Items?.Add(new MenuFlyoutSeparator());

            // Add suggested ANSI encodings
            var appAndSystemANSIEncodings = new HashSet<Encoding>();

            if (EncodingUtility.TryGetSystemDefaultANSIEncoding(out var systemDefaultANSIEncoding))
            {
                appAndSystemANSIEncodings.Add(systemDefaultANSIEncoding);
            }
            if (EncodingUtility.TryGetCurrentCultureANSIEncoding(out var currentCultureANSIEncoding))
            {
                appAndSystemANSIEncodings.Add(currentCultureANSIEncoding);
            }

            if (appAndSystemANSIEncodings.Count > 0)
            {
                foreach (var encoding in appAndSystemANSIEncodings)
                {
                    AddEncodingItem(encoding, reopenWithEncoding, saveWithEncoding);
                }
                reopenWithEncoding.Items?.Add(new MenuFlyoutSeparator());
                saveWithEncoding.Items?.Add(new MenuFlyoutSeparator());
            }
            
            // Add Unicode encodings
            var unicodeEncodings = new List<Encoding>
            {
                new UTF8Encoding(false), // "UTF-8"
                new UTF8Encoding(true), // "UTF-8-BOM"
                new UnicodeEncoding(false, true), // "UTF-16 LE BOM"
                new UnicodeEncoding(true, true), // "UTF-16 BE BOM"
            };

            foreach (var encoding in unicodeEncodings)
            {
                AddEncodingItem(encoding, reopenWithEncoding, saveWithEncoding);
            }

            // Add legacy ANSI encodings
            var ANSIEncodings = EncodingUtility.GetAllSupportedANSIEncodings();
            if (ANSIEncodings.Length > 0)
            {
                reopenWithEncoding.Items?.Add(new MenuFlyoutSeparator());
                saveWithEncoding.Items?.Add(new MenuFlyoutSeparator());

                var reopenWithEncodingOthers = new MenuFlyoutSubItem()
                {
                    Text = _resourceLoader.GetString("TextEditor_EncodingIndicator_FlyoutItem_MoreEncodings"),
                    FlowDirection = FlowDirection.RightToLeft,
                };

                var saveWithEncodingOthers = new MenuFlyoutSubItem()
                {
                    Text = _resourceLoader.GetString("TextEditor_EncodingIndicator_FlyoutItem_MoreEncodings"),
                    FlowDirection = FlowDirection.RightToLeft,
                };

                foreach (var encoding in ANSIEncodings)
                {
                    AddEncodingItem(encoding, reopenWithEncodingOthers, saveWithEncodingOthers);
                }

                reopenWithEncoding.Items?.Add(reopenWithEncodingOthers);
                saveWithEncoding.Items?.Add(saveWithEncodingOthers);
            }

            EncodingSelectionFlyout.Items?.Add(reopenWithEncoding);
            EncodingSelectionFlyout.Items?.Add(saveWithEncoding);
        }