隐藏字符串

时间:2018-05-11 06:42:16

标签: c# wpf string email hyperlink

我正在构建一个字符串,我必须隐藏一个电子邮件地址。

string StatusText = "Please reach out here for access."

只是想知道如何在字符串中隐藏电子邮件地址。我希望“这里”成为电子邮件地址。我必须将StatusText绑定到Textblock,以便当用户单击“here”时,Outlook邮件应该打开。

这是详细的代码:

View.xaml

<TextBlock   Text="{Binding StatusText}" />

ViewModel.cs

private string _statusText;
public string StatusText
{
            get { return _statusText; }
            set { SetProperty(ref _statusText, value); }
}


DisplayMessages()
{
  //based on the boolean value, Status Text is set
  //When the case is "NoAccess", I want the "here" of StatusText to be clickable and show the mailaddress as abc@abc.com
  switch(Flag)
   case IsReady:
       StatusText = "Application is Ready";
     break;
   case NoAccess:
     StatusText = "Please reach out here. No access can be provided."
    break;
}

2 个答案:

答案 0 :(得分:2)

我想利用XAML

<TextBlock>
    <Run Text="Please reach out "/>
    <Hyperlink NavigateUri="YOUR_URL_FOR_OUTLOOK" RequestNavigate="Hyperlink_RequestNavigate">here</Hyperlink>
    <Run Text=" for access."/>
</TextBlock>

在代码背后

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
{
    Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri));
    e.Handled = true;
}

使用此方法的主要好处是您可以使用Binding将您的emailId绑定到Hyperlink

您的输出在Viewer中将如下所示 enter image description here

答案 1 :(得分:2)

第1步: 在Window.Resources中添加以下XAML代码。

<local:DataTemplateSelectorExt x:Key="Selector"></local:DataTemplateSelectorExt>

<DataTemplate x:Key="NoAccess">
        <TextBlock Background="Green">
            <Run Text="Please reach out "/>
            <Hyperlink NavigateUri="abc@gmail.com" RequestNavigate="Hyperlink_RequestNavigate">here</Hyperlink>
            <Run Text=" for access."/>
        </TextBlock>
    </DataTemplate>

第2步: 使用以下代码段替换TextBlock XAML代码。

<Label Content="{Binding StatusText}" ContentTemplateSelector="{Binding Source={StaticResource Selector}}" />

第3步: 在MainWindow类中添加以下方法。

private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
    {
        Popup popup = new Popup { IsOpen = true, Width = 100, Height = 50, Placement = PlacementMode.Mouse };
        popup.Child = new TextBlock { Text = (sender as Hyperlink).NavigateUri.ToString(), Background = Brushes.Green };
        popup.IsOpen = true;            
    }

第4步: 最后在项目中添加以下类。

public class DataTemplateSelectorExt : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item == null) return (DataTemplate)null;

        FrameworkElement frameworkElement = container as FrameworkElement;

        if (item.Equals("Please reach out here. No access can be provided."))
            return Application.Current.MainWindow.FindResource("NoAccess") as DataTemplate;

            return null;
    }
}

希望,这符合您的要求。