Value Converter不在Xamarin工作

时间:2017-01-31 15:23:47

标签: xamarin xamarin.forms ivalueconverter

这里有点困惑,我似乎已经遵循了允许我使用值转换器的步骤。

我用一把钥匙定义了我的转换器,如下:

using HaveYouSeenMeApp.Models;
using HaveYouSeenMeApp.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HaveYouSeenMeApp.Controllers
{
    public class HomeController : Controller
    {
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            ApplicationDbContext db = new ApplicationDbContext();
            List<ImageViewModel> ImageViews = new List<ImageViewModel>();
            var ImageList = (from cust in db.PetPhotoSS select new { cust.PetPhotoName, cust.Content }).ToList();

            foreach(var item in ImageList)
            {
                ImageViewModel objcvm = new ImageViewModel();
                objcvm.PetPhotoName = item.PetPhotoName;
                objcvm.Content = item.Content;
                ImageViews.Add(objcvm);
            }
            return View(ImageViews);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

然后我在我的XAML中使用我的转换器,如下:

@model IEnumerable<HaveYouSeenMeApp.Models.ViewModels.ImageViewModel>

@{
    ViewBag.Title = "Home Page";
}

<div class="jumbotron">
    <h1>ASP.NET</h1>
    <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
    <p><a href="http://asp.net" class="btn btn-primary btn-large">Learn more &raquo;</a></p>
</div>

<div class="row">
    <div class="col-md-4">
        <h2>Getting started</h2>
        <p>
            <table>
                <tr>
                   <th> @Html.DisplayNameFor(model => model.PetPhotoName)</th>
                    <th>
                    @Html.DisplayNameFor(model => model.Content)</th>
                </tr>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.PetPhotoName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Content)
                        </td>
                    </tr>
                }
            </table>
        </p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
    </div>
    <div class="col-md-4">
        <h2>Get more libraries</h2>
        <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301866">Learn more &raquo;</a></p>
    </div>
    <div class="col-md-4">
        <h2>Web Hosting</h2>
        <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
        <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
    </div>
</div>

这是我的实际转换器代码:

    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage Title="Article"
                 xmlns="http://xamarin.com/schemas/2014/forms"
                           xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:controls="clr-namespace:XamarinMobile.Controls;assembly=XamarinMobile"
                 xmlns:converters="clr-namespace:XamarinMobile.Converters;assembly=XamarinMobile"
                 x:Class="XamarinMobile.ArticlePage">
      <ContentPage.Resources>
        <ResourceDictionary>
          <converters:FontSizeConverter x:Key="FontSizeMapper"></converters:FontSizeConverter>
        </ResourceDictionary>
      </ContentPage.Resources>

然后我在我的值转换器中设置了一个断点,但它从未命中过。我有什么明显的遗漏吗?我很确定我按照指示去了发球台。

2 个答案:

答案 0 :(得分:3)

由于Gerald Versluis所说的话,你的断点没有被击中。你的绑定被打破了。你的约束力是:绑定到名为&#34; 10&#34;在BindingContext上,使用Converter FontSizeMapper,向其传递额外的ConverterParameter 10。&#34; 10&#34;不是有效的属性名称,因此绑定正在破坏。如果您查看日志,您应该看到类似于以下消息:&#34;绑定:&#39; 10&#39;找不到......&#34;

解决问题的一种方法是删除&#34;路径&#34;您尝试绑定并仅使用ConverterParameter(假设您不需要绑定到任何真实属性):

FontSize="{Binding Converter={StaticResource FontSizeMapper}, ConverterParameter=20}"

请注意,您需要使用转换器中的parameter,而不是value(例如if (parameter is double))。

如果您不需要绑定到任何属性,另一种解决方法是使用自定义标记扩展名。

[ContentProperty("FontSize")]
public class FontSizeMapperExtension : IMarkupExtension
{
    public double FontSize { get; set; }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        return App.NormalizeFontSize(FontSize);
    }
}

然后你可以在你的XAML中使用它,如:

FontSize="{converters:FontSizeMapper FontSize=10}

修改

绑定到对象上的属性的示例:

public class YourViewModel
{
    public double VMFontSize { get; set; }
}

public partial class ArticlePage : ContentPage
{
    public ArticlePage()
    {
        InitializeComponent();

        // NOTE: You'd probably get your view-model another way
        var viewModel = new YourViewModel { VMFontSize = 10 };
        BindingContext = viewModel;
    }
}

现在您的视图模型已设置为绑定上下文,您可以设置绑定,如:

FontSize="{Binding VMFontSize, Converter={StaticResource FontSizeMapper}}"

这就是说:将标签上的FontSize属性绑定到当前BindingContext(您的视图模型)上的VMFontSize属性,使用转换器在视图模型的VMFontSize和Label的FontSize。我将ConverterParameter留在了这里,因为在这个例子中它并不是真的需要,但如果你需要,你可以传递一个。

答案 1 :(得分:1)

我会以不同的方式执行此操作,使用自定义附加属性,请在此处查看有关附加属性的更多信息https://developer.xamarin.com/guides/xamarin-forms/xaml/attached-properties/

以下是您的场景的示例,首先我们需要定义一个附加属性,它可以在任何类中,我称之为我的FontHelper

namespace App23
{
    public static class FontHelper
    {
        public static readonly BindableProperty FontSizeProperty =
            BindableProperty.CreateAttached("FontSize", typeof(double), typeof(FontHelper), 0d, propertyChanging:OnPropertyChanging);

        public static bool GetFontSize(BindableObject view)
        {
            return (bool)view.GetValue(FontSizeProperty);
        }

        public static void SetFontSize(BindableObject view, bool value)
        {
            view.SetValue(FontSizeProperty, value);
        }

        private static void OnPropertyChanging(BindableObject bindable, object oldValue, object newValue)
        {
            if (bindable is Label)
            {
                var label = bindable as Label;
                double fontSize = (double)newValue;
                // normalize your font size here
                label.FontSize = fontSize;
            }
        }
    }
}

然后在XAML中使用它,它看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App23"
             x:Class="App23.MainPage">

  <Label Text="Welcome to Xamarin Forms!"
           VerticalOptions="Center"
           HorizontalOptions="Center" local:FontHelper.FontSize="50"/>

</ContentPage>