UWP goBack到缓存页面关闭应用程序

时间:2016-08-16 08:32:14

标签: c# windows-runtime uwp

我有一个RootPage作为根视图。在我的RootPage中,我添加了这个以支持后退按钮:

SystemNavigationManager.GetForCurrentView().BackRequested += SystemNavigationManager_BackRequested;

private void SystemNavigationManager_BackRequested(object sender, BackRequestedEventArgs e) {
    bool handled = e.Handled;
    if (this.AppFrame == null)
        return;

    if (this.AppFrame.CanGoBack && !handled) {
        // If not, set the event to handled and go back to the previous page in the app.
        handled = true;
        this.AppFrame.GoBack();
    }
    e.Handled = handled;
}

这可以按预期工作(如果有用户,用户将转到上一页)。但是,当我为一个页面启用缓存时,它不再起作用了:

this.NavigationCacheMode = NavigationCacheMode.Enabled;

我也试过Required,但结果相同。

当我按下后退按钮时,它只关闭应用程序而不是返回。回过头来的代码是执行的,所以我不知道自己做错了什么。

更新

我找到了例外。我有一个自定义TextBox,用文本符号和空格填充文本。这基本上是创建异常的原因:

this.Loaded += (sender, args) => {
   Text = "$ ";
};

注意:它不是破坏应用程序的货币符号,而是我在返回此页面时设置文本在已加载中的事实(页面缓存现在应该处理这个,所以我想这是冲突的)< / p>

更新2

重现问题的代码示例。自定义TextBox:     public class RegexTextBox:TextBox {         private string regex =&#34;&#34 ;;         私有字符串previousText =&#34;&#34 ;;         private int previousSelectionStart = 0;

    public RegexTextBox() {
        this.Loaded += (sender, args) => {
            switch(RegexType) {
                case RegexTextBoxTypes.Number:
                    regex = "^([0-9]+|)$";
                    break;
                case RegexTextBoxTypes.Currency:
                    regex = "^€ ([0-9]+|)$";
                    Text = "€ ";
                    break;
            }

            this.TextChanging += input_TextChanging;
            this.SelectionChanged += input_SelectionChanged;
            previousText = Text;
            previousSelectionStart = SelectionStart;
        };
    }

    private void input_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args) {
        if (regex.Length > 0) {
            if (!Regex.IsMatch(Text, regex)) {
                Text = previousText;
                SelectionStart = previousSelectionStart;
            }

            previousText = Text;
        }
    }

    private void input_SelectionChanged(object sender, RoutedEventArgs e) {
        previousSelectionStart = SelectionStart;
    }

    public bool isEmpty() {
        if(RegexType == RegexTextBoxTypes.Currency) {
            return !Regex.IsMatch(Text, "^€ ([0-9]+)$");
        }

        return Text.Length == 0;
    }

    public void ClearText() {
        if(RegexType == RegexTextBoxTypes.Currency) {
            Text = "€ ";
        } else {
            Text = "";
        }
    }

    public string GetPlainText() {
        if (RegexType == RegexTextBoxTypes.Currency) {
            return Text.Substring(2, Text.Length - 2);
        }

        return Text;
    }

    public RegexTextBoxTypes RegexType {
        get { return (RegexTextBoxTypes)GetValue(RegexTypeProperty); }
        set { SetValue(RegexTypeProperty, value); }
    }

    public static readonly DependencyProperty RegexTypeProperty =
        DependencyProperty.Register("RegexType", typeof(RegexTextBoxTypes), typeof(RegexTextBox), new PropertyMetadata(RegexTextBoxTypes.Normal));
}

public enum RegexTextBoxTypes {
    Normal,
    Number,
    Currency
}

在你的xaml中将它放在你的网格中(控制命名空间指向自定义TextBox的位置):

        <controls:RegexTextBox
            InputScope="Number"
            RegexType="Currency"/>

然后在页面的构造函数中输入:

this.NavigationCacheMode = NavigationCacheMode.Required;

确保自定义TextBox中填充了一些数字。然后导航到新页面并返回,当导航回来时,由于未被捕获的异常,它应关闭应用程序(尽管视觉工作室赢了并且表明存在异常)。

0 个答案:

没有答案