Xamarin.forms Xamarin表单:如何使用绑定到ListView来组合来自不同get web服务的值

Xamarin.forms Xamarin表单:如何使用绑定到ListView来组合来自不同get web服务的值,xamarin.forms,Xamarin.forms,亲爱的 我正在调用xamarin表单项目中的两个web服务。从第一个web服务中,我得到一个图像的根URL,从第二个web服务中,我得到剩余的URL部分。只有第二个web服务响应使用ItemsSource属性与listview连接。我的根url是d1kv7s9g8y3npv.cloudfront.net。目前,我在代码中硬编码了rooturl值 我的图像代码: <Image Source

亲爱的

我正在调用xamarin表单项目中的两个web服务。从第一个web服务中,我得到一个图像的根URL,从第二个web服务中,我得到剩余的URL部分。只有第二个web服务响应使用ItemsSource属性与listview连接。我的根url是d1kv7s9g8y3npv.cloudfront.net。目前,我在代码中硬编码了rooturl值

我的图像代码:

                       <Image 
                            Source="{Binding remainingUrl , StringFormat='d1kv7s9g8y3npv.cloudfront.net{0:F0}'}"
                            HorizontalOptions="Start"
                            VerticalOptions="Start"
                            WidthRequest="50" /> 

那么,我如何在没有硬编码的情况下将根url添加到此图像代码中呢


提前感谢

您的问题有一个简单的解决方案。
只需创建一个属性,该属性将结合URL的两个部分:

// In your ViewModel
public string CombinedUrl => $"{firstPart}/{secondPart}";

// In your XAML
<Image 
   Source="{Binding CombinedUrl}"
   HorizontalOptions="Start"
   VerticalOptions="Start"
   WidthRequest="50" /> 
//在您的ViewModel中

公共字符串CombinedUrl=>$“{firstPart}/{secondPart}”; //在您的XAML中

请注意命名约定,从您的示例中,我可以看到您使用小写名称“remaingurl”来表示违反规则的属性。

您的问题有一个简单的解决方案。
只需创建一个将URL的两部分结合在一起的属性:

// In your ViewModel
public string CombinedUrl => $"{firstPart}/{secondPart}";

// In your XAML
<Image 
   Source="{Binding CombinedUrl}"
   HorizontalOptions="Start"
   VerticalOptions="Start"
   WidthRequest="50" /> 
//在您的ViewModel中

公共字符串CombinedUrl=>$“{firstPart}/{secondPart}”; //在您的XAML中

请注意命名约定,从您的示例中,我可以看到您使用小写名称“remainingUrl”来表示违反规则的属性。

最后,使用Ivalueconverter解决此问题。 mainurl使用Application.Current.Properties存储在变量中

第二个web服务是项目列表。所以我需要加入这两个url并在UI中显示它。为此,我使用IValueConverter

我的转换器代码:

public class UrlsLinkconverter : IValueConverter
{
#region IValueConverter implementation
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (value == null)
        return string.Empty;

    string rootUrl = Application.Current.Properties["rootUrl"].ToString();
    string fullUrl = String.Concat("http://" + rootUrl,value.ToString())
    Debug.WriteLine("fullUrl::>>>>"+fullUrl);
    return fullUrl.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    throw new NotImplementedException();
}
#endregion
}

最后,使用Ivalueconverter解决该问题。 mainurl使用Application.Current.Properties存储在变量中

第二个web服务是项目列表。所以我需要加入这两个url并在UI中显示它。为此,我使用IValueConverter

我的转换器代码:

public class UrlsLinkconverter : IValueConverter
{
#region IValueConverter implementation
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if (value == null)
        return string.Empty;

    string rootUrl = Application.Current.Properties["rootUrl"].ToString();
    string fullUrl = String.Concat("http://" + rootUrl,value.ToString())
    Debug.WriteLine("fullUrl::>>>>"+fullUrl);
    return fullUrl.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    throw new NotImplementedException();
}
#endregion
}

为什么不创建一个将组合两个URL部分并简单绑定到它的属性呢?您的意思是这样的:公共字符串FirstLastName{get{return FirstName+“”+LastName;}}为什么不创建一个将组合两个URL部分并简单绑定到它的属性呢?您的意思是这样的:公共字符串FirstLastName{get return FirstName++“”+LastName;}}}公共字符串组合URL=>$“{firstPart}/{secondPart}"; 需要在模型类中添加此代码,对吗?我更新了答案,希望现在可以更容易理解我的意思。我没有使用viewmodel类。是否可以在model或xaml.cs类中应用此选项?如果您绑定到您的模型或xaml.cs并且它正在工作,那么-是的,但是如果您希望遵循MVVM.public string CombinedUrl=>$“{firstPart}/{secondPart}”,则应该绑定到ViewModel;需要在模型类中添加此代码,对吗?我更新了答案,希望现在可以更容易理解我的意思。我没有使用viewmodel类。是否可以在model或xaml.cs类中应用此功能?如果您绑定到您的model或xaml.cs,并且它正在工作,那么-是的,但是如果您希望遵循MVVM,则应该绑定到ViewModel。