Xamarin在图像上形成URL绑定

时间:2019-01-30 10:32:34

标签: xaml xamarin xamarin.forms

我的xaml中有一个Image,其中的源是Iam绑定的URL。json中的URL类似于:“ /images/Uploads/e0111.png ”。我的通用值存储类中的URL为CommonValues.URL。绑定时如何在json之前添加此“ CommonValues.URL”?这样Image的来源就是http://example.com//images/Uploads/e0111.png。?

2 个答案:

答案 0 :(得分:1)

如果您需要Uri

var myUrl= new Uri(CommonValues.URL + "images/Uploads/e0111.png");

如果string

var myUrl=CommonValues.URL + "images/Uploads/e0111.png";

或者您可以在ViewModelPage

中这样做
public string Url => string.Format("{0}{1}", CommonValues.URL,"/images/Uploads/e0111.png");

然后使用XAML:

<Button Text="{Binding Url}"/>

答案 1 :(得分:1)

您可以使用转换器,这将允许您在所有视图/应用程序中重复使用

import flask
from flask import Flask, render_template, jsonify, request,send_file
from flask_cors import CORS, cross_origin
import numpy as np
import json
import os
#import pandas

print(flask.__version__)

app = Flask(__name__,template_folder='.')
CORS(app, origins="http://127.0.0.1:8080", allow_headers=[
    "Content-Type", "Authorization", "Access-Control-Allow-Credentials"],
    supports_credentials=True)



@app.route('/', methods=['POST'])
def index():
    return render_template('index.html')

@app.route('/api/predictions',methods=['POST'])
def predictions():
    # Cette fonction
    params = request.get_json(force=True)
    print(params)
    with open('predictions.json', encoding='utf8') as f:
        data = json.load(f)
        return jsonify(data)

@app.route('/api/prediction_lime',methods=['POST'])
def prediction():
    # Cette fonction
    params = request.get_json(force=True)
    print(params)
    with open('prediction_lime.json', encoding='utf8') as f:
        data = json.load(f)
        return jsonify(data)

然后,在您的页面中:

public class UrlConverter : IValueConverter
{
    #region IValueConverter implementation

    public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var test = value as string; 
        if (!string.IsNullOrEmpty(test))  
        {
            return CommonValues.URL + test;
        }
        return false;
    }

    public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException ();
    }

    #endregion
}