Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LinkButton没有';我不能在Silverlight工作_C#_Silverlight - Fatal编程技术网

C# LinkButton没有';我不能在Silverlight工作

C# LinkButton没有';我不能在Silverlight工作,c#,silverlight,C#,Silverlight,我调用了一个方法来创建链接按钮。然而问题是,传递字段名时,实际的“字段到链接”按钮实际上给了我一个死链接 方法如下: private static DataGridTemplateColumn CreateHyperlink(string fieldName) { DataGridTemplateColumn column = new DataGridTemplateColumn(); column.Header = "";

我调用了一个方法来创建链接按钮。然而问题是,传递字段名时,实际的“字段到链接”按钮实际上给了我一个死链接

方法如下:

     private static DataGridTemplateColumn CreateHyperlink(string fieldName)
    {
        DataGridTemplateColumn column = new DataGridTemplateColumn();

        column.Header = "";

        string link = @"http://www.amazon.com/gp/product/" + fieldName;

        StringBuilder sb = new StringBuilder();
        sb.Append("<DataTemplate ");
        sb.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
        sb.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
        sb.Append("xmlns:src='clr-namespace:Silverlight1.Classes;assembly=Silverlight1.Classes'>");
        sb.Append("<HyperlinkButton ");
        sb.Append("TargetName= '_blank' ");
        sb.Append("Content = 'Link' ");
        sb.Append("NavigateUri =" +"'"+ link +"'");
        sb.Append(" />");
        sb.Append("</DataTemplate>");

        column.CellTemplate = (DataTemplate)XamlReader.Load(sb.ToString());
        column.IsReadOnly = false;
        return column;
    }
私有静态DataGridTemplateColumn CreateHyperlink(字符串字段名)
{
DataGridTemplateColumn=新DataGridTemplateColumn();
列。标题=”;
字符串链接=@“http://www.amazon.com/gp/product/“+字段名;
StringBuilder sb=新的StringBuilder();
某人加上(“”);
某人加上(“”);
某人加上(“”);
column.CellTemplate=(DataTemplate)XamlReader.Load(sb.ToString());
column.IsReadOnly=false;
返回列;
}
这就是所谓的

dgOrder.Columns.Add(CreateHyperlink(“asin”)

它是从WCF Silverlight enable数据服务中提取的。
如何传递内容而不是字段名?

如果不传递内容,则传递字段名,但创建一个
绑定。您还需要一个
IValueConverter
的实现

让我们从一个
IValueConverter
开始,您需要一个以“field”的字符串值(我假设您指的是绑定到网格中的行的对象的属性)为前缀的东西http://www.amazon.com/gp/product/“
以形成完整的URL

public class UrlPrefixConverter : IValueConverter
{
     public string Prefix {get; set;}
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        { 
            return new Uri(Prefix + value.ToString(), UriKind.Absolute);
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
在App.xaml中放置此实例:-

 <Application.Resources>
      <local:UrlPrefixConverter Prefix="http://www.amazon.com/gp/product/" x:Key="AmazonPrefixConverter" />
 </Application.Resources>

现在,您的创建方法可以如下所示:-

    private static DataGridTemplateColumn CreateHyperlink(string fieldName)
    {
        DataGridTemplateColumn column = new DataGridTemplateColumn();

        column.Header = "";

        string template = @"<DataTemplate
                xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" >
                <HyperlinkButton TargetName=""_blank"" Content=""Link""
                     NavigateUri=""{{Binding {0}, Converter={{StaticResource AmazonPrefixConverter}}}}"" />
             </DataTemplate>"; 

        column.CellTemplate = (DataTemplate)XamlReader.Load(String.Format(template, fieldName));
        column.IsReadOnly = false;
        return column;
    }
私有静态DataGridTemplateColumn CreateHyperlink(字符串字段名)
{
DataGridTemplateColumn=新DataGridTemplateColumn();
列。标题=”;
字符串模板=@“
"; 
column.CellTemplate=(DataTemplate)XamlReader.Load(String.Format(template,fieldName));
column.IsReadOnly=false;
返回列;
}

您不传递内容,而是传递字段名,但创建一个
绑定。您还需要一个
IValueConverter
的实现

让我们从一个
IValueConverter
开始,您需要一个以“field”的字符串值(我假设您指的是绑定到网格中的行的对象的属性)为前缀的东西http://www.amazon.com/gp/product/“
以形成完整的URL

public class UrlPrefixConverter : IValueConverter
{
     public string Prefix {get; set;}
     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        { 
            return new Uri(Prefix + value.ToString(), UriKind.Absolute);
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
在App.xaml中放置此实例:-

 <Application.Resources>
      <local:UrlPrefixConverter Prefix="http://www.amazon.com/gp/product/" x:Key="AmazonPrefixConverter" />
 </Application.Resources>

现在,您的创建方法可以如下所示:-

    private static DataGridTemplateColumn CreateHyperlink(string fieldName)
    {
        DataGridTemplateColumn column = new DataGridTemplateColumn();

        column.Header = "";

        string template = @"<DataTemplate
                xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" >
                <HyperlinkButton TargetName=""_blank"" Content=""Link""
                     NavigateUri=""{{Binding {0}, Converter={{StaticResource AmazonPrefixConverter}}}}"" />
             </DataTemplate>"; 

        column.CellTemplate = (DataTemplate)XamlReader.Load(String.Format(template, fieldName));
        column.IsReadOnly = false;
        return column;
    }
私有静态DataGridTemplateColumn CreateHyperlink(字符串字段名)
{
DataGridTemplateColumn=新DataGridTemplateColumn();
列。标题=”;
字符串模板=@“
"; 
column.CellTemplate=(DataTemplate)XamlReader.Load(String.Format(template,fieldName));
column.IsReadOnly=false;
返回列;
}

App.xaml中的实例有问题,我最终使用了它local'是一个未声明的前缀。@Joe:“local:”是用于声明应用程序主命名空间的普通xml别名的实际选择。在顶级的
应用程序
元素中,您会有类似于
xmlns:local=“clr namespace:yourAppsNamespace
的内容。现在我得到
在类型“Binding”中找不到属性“asin Converter”。[行:4位置:34]
@Joe:
{0}之间缺少一个逗号
Converter
调整答案。我最终使用了这个,但App.xaml中的实例有问题。“local”是一个未声明的前缀。@Joe:“local:“是用于声明应用程序主命名空间的常规xml别名的实际选择。在顶级
应用程序
元素中,您可能会有类似于
xmlns:local=“clr namespace:yourAppsNamespace
的内容。现在我得到
属性“asin Converter”在类型“Binding”中找不到。[行:4位置:34]
@Joe:在
{0}
Converter
之间缺少一个逗号。