- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Xamarin.Forms 2.4 构建社交帖子共享应用程序,它与我的 API 对话。
我有 PostsPage,它使用 PostDataViewModel 将 ObservableCollection 加载到 PostsPage 中定义的 ListView。 ListView 的数据模板指向 View 文件。但是,在 Post View Template 中,我可以绑定(bind)到 My Post Model 的各个属性,但绑定(bind)到 ViewModel 上存在的命令不起作用。我试过 x:Reference 没有运气。
我的模型:
using System;
using System.Collections.Generic;
namespace SOD_APP_V2.Model
{
public class PostDataModel
{
public string ID { get; set; }
public string Title { get; set; }
public string Message { get; set; }
public string Image { get; set; }
public string SocialMedia { get; set; }
public string AvailableTime { get; set; }
public string Audiences { get; set; }
public string Topics { get; set; }
public List<PostVersion> Versions { get; set; }
public PostDataModel PostDetails
{
get
{
return this;
}
}
}
public class PostVersion
{
public string SocialMediaID { get; set; }
public string IconPath { get; set; }
public int CharacterCount { get; set; }
public string Message { get; set; }
}
}
namespace SOD_APP_V2.ViewModel
{
public class PostDataViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
//Bindable properties
public ObservableCollection<PostDataModel> PostDataCollection { get; set; }
public ICommand LoadMorePostsCommand { get; private set; }
public ICommand RejectPostCommand { get; private set; }
public static JObject SocialmediaSites { get; set; }
public object SelectedItem { get; set; }
bool isLoadMoreEnabled;
public bool IsLoadMoreEnabled
{
get
{
return isLoadMoreEnabled;
}
set
{
if (isLoadMoreEnabled != value)
{
isLoadMoreEnabled = value;
OnPropertyChanged((nameof(IsLoadMoreEnabled)));
}
}
}
string pageTitle;
public string PageTitle
{
get
{
return pageTitle;
}
set
{
if (pageTitle != value)
{
pageTitle = value;
OnPropertyChanged(nameof(PageTitle));
}
}
}
int currentPage = 1;
public int CurrentPage
{
get
{
return currentPage;
}
set
{
if(currentPage != value)
{
currentPage = value;
OnPropertyChanged(nameof(CurrentPage));
}
}
}
public PostDataViewModel()
{
PostDataCollection = new ObservableCollection<PostDataModel>();
SocialmediaSites = default(JObject);
IsLoadMoreEnabled = true;
LoadMorePostsCommand =
new Command(async () => await GetPosts(), () => IsLoadMoreEnabled);
RejectPostCommand = new Command<PostDataModel>((post) =>
{
System.Diagnostics.Debug.WriteLine("Reject command executed");
System.Diagnostics.Debug.WriteLine("Post ID: " + post.ID);
});
string deployment = ConfigController.GetDeploymentName(ApiController.DeploymentDomain);
MessagingCenter.Subscribe<PostsPage, JArray>(this, "translations", (sender, arg) => {
PageTitle = (arg[0].ToString() != "") ? arg[0].ToString() : "Posts from " + deployment;
});
if (deployment != null)
{
//TODO: lang packs
PageTitle = "Posts from " + deployment;
}
}
public async Task<bool> GetPosts()
{
...
}
protected virtual void OnPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(nameof(propertyName)));
}
}
}
<?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:SOD_APP_V2"
xmlns:view="clr-namespace:SOD_APP_V2.View;assembly=SOD_APP_V2"
xmlns:viewModel="clr-namespace:SOD_APP_V2.ViewModel;assembly=SOD_APP_V2"
xmlns:controls="clr-namespace:SOD_APP_V2.Controls;assembly=SOD_APP_V2"
x:Class="SOD_APP_V2.PostsPage" x:Name="PostsPage"
>
<ContentPage.BindingContext>
<viewModel:PostDataViewModel/>
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
<StackLayout Orientation="Vertical" Padding="10, 5, 10, 5">
<Label x:Name="titleLabel" Text="{Binding PageTitle}"
VerticalOptions="Start"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
BackgroundColor="Transparent"
HorizontalOptions="CenterAndExpand" />
<controls:InfiniteListView x:Name="listView"
SelectedItem="{Binding SelectedItem,Mode=TwoWay}"
IsLoadMoreItemsPossible="{Binding IsLoadMoreEnabled}"
LoadMoreInfiniteScrollCommand="{Binding LoadMorePostsCommand}"
IsEnabled="true"
IsBusy="{Binding IsBusy}"
HasUnevenRows="true"
ItemsSource="{Binding PostDataCollection}"
SeparatorVisibility="None">
<controls:InfiniteListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<view:PostViewTemplate/>
</ViewCell>
</DataTemplate>
</controls:InfiniteListView.ItemTemplate>
</controls:InfiniteListView>
</StackLayout>
<StackLayout HorizontalOptions="FillAndExpand"
VerticalOptions="End">
<Label x:Name="infoLabel" Text="test"
Opacity="0"
TextColor="White"
BackgroundColor="#337ab7"
HorizontalTextAlignment="Center">
</Label>
</StackLayout>
</StackLayout>
</ContentPage.Content>
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:model="clr-namespace:SOD_APP_V2.Model;assembly=SOD_APP_V2"
xmlns:controls="clr-namespace:SOD_APP_V2.Controls;assembly=SOD_APP_V2"
x:Class="SOD_APP_V2.View.PostViewTemplate">
<ContentView.Resources>
<ResourceDictionary>
<Style TargetType="controls:AwesomeButton">
<Setter Property="BorderWidth" Value="1"/>
<Setter Property="TextColor" Value="White"/>
<Setter Property="BorderRadius" Value="7"/>
<Setter Property="FontFamily" Value="FontAwesome"/>
</Style>
</ResourceDictionary>
</ContentView.Resources>
<ContentView.Content>
<Frame HasShadow="false" CornerRadius="5" IsClippedToBounds="true" OutlineColor="#09478e" Padding="0" Margin="10">
<Grid
Padding="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="1" />
<ColumnDefinition Width="1" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="1"></RowDefinition>
<RowDefinition Height="100"></RowDefinition>
<RowDefinition Height="1"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="40"></RowDefinition>
<RowDefinition Height="1"></RowDefinition>
</Grid.RowDefinitions>
<StackLayout Orientation="Horizontal" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="6" BackgroundColor="#B0C4DB" Padding="5">
<Label x:Name="postTitle" Text="{Binding Title}" TextColor="#09478e" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
<StackLayout Orientation="Horizontal" VerticalOptions="CenterAndExpand" HorizontalOptions="End">
<controls:AwesomeButton Clicked="OnAvailableTimePopupClicked" CommandParameter="{Binding AvailableTime}" Text="" TextColor="#09478e" BorderWidth="0" Margin="-5" BackgroundColor="Transparent" WidthRequest="36" FontSize="24"></controls:AwesomeButton>
<controls:AwesomeButton Clicked="OnAudiencesPopupClicked" CommandParameter="{Binding Audiences}" Text="" TextColor="#09478e" BorderWidth="0" Margin="-5" BackgroundColor="Transparent" WidthRequest="36" FontSize="24"></controls:AwesomeButton>
<controls:AwesomeButton Clicked="OnTopicsPopupClicked" CommandParameter="{Binding Topics}" Text="" TextColor="#09478e" BorderWidth="0" Margin="-5" BackgroundColor="Transparent" WidthRequest="36" FontSize="24"></controls:AwesomeButton>
</StackLayout>
</StackLayout>
<controls:RepeaterView Grid.Row="1" Grid.Column="2"
ItemsSource="{Binding Versions}"
NoOfColumns="3"
>
<controls:RepeaterView.ItemTemplate>
<DataTemplate>
<StackLayout Spacing="0" >
<Label Text="{Binding Message}" IsVisible="false"/>
<Image Source="{Binding IconPath}">
<Image.GestureRecognizers>
<TapGestureRecognizer
Tapped="OnMessageVersionClicked"
NumberOfTapsRequired="1" />
</Image.GestureRecognizers>
</Image>
</StackLayout>
</DataTemplate>
</controls:RepeaterView.ItemTemplate>
</controls:RepeaterView>
<Image BackgroundColor="White" Grid.Row="3" Grid.Column="2" Source="{Binding Image}" VerticalOptions="FillAndExpand"/>
<Label x:Name="postMessage" BackgroundColor="White" Grid.Row="3" Grid.Column="3" Text="{Binding Message}" TextColor="#09478e" VerticalOptions="FillAndExpand" />
<controls:AwesomeButton Clicked="OnSharePostClicked" CommandParameter="{Binding ID}" BackgroundColor="#5cb85c" Grid.Row="5" Grid.Column="2" Text=" Share" BorderColor="#5cb85c"></controls:AwesomeButton>
<controls:AwesomeButton Clicked="OnSchedulePostClicked" CommandParameter="{Binding ID}" BackgroundColor="#5bc0de" Grid.Row="5" Grid.Column="3" Text=" Schedule" BorderColor="#5bc0de"></controls:AwesomeButton>
<controls:AwesomeButton Clicked="OnEditPostClicked" CommandParameter="{Binding PostDetails}" TextColor="#09478e" BackgroundColor="White" Grid.Row="6" Grid.Column="2" Text=" Edit" BorderColor="#09478e"></controls:AwesomeButton>
<controls:AwesomeButton Command="{Binding Path=DataContext.RejectPostCommand}" CommandParameter="{Binding}" BackgroundColor="#d9534f" Grid.Row="6" Grid.Column="3" Text=" Reject" BorderColor="#d9534f"></controls:AwesomeButton>
<!-- Inner Border -->
<BoxView Grid.Row="2" Grid.RowSpan="3" Grid.Column="1" BackgroundColor="#09478e"></BoxView>
<BoxView Grid.Row="2" Grid.RowSpan="3" Grid.Column="4" BackgroundColor="#09478e"></BoxView>
<BoxView Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="2" BackgroundColor="#09478e"></BoxView>
<BoxView Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="4" BackgroundColor="#09478e"></BoxView>
</Grid>
</Frame>
</ContentView.Content>
最佳答案
将 PostViewTemplate XAML 移动到 PostsPage 可能有效,但现在您没有可重用的模板。
您可以通过对原始代码进行以下 3 个小改动来创建可重用的解决方案。
将可绑定(bind)属性添加到 PostViewTemplate 代码隐藏,如下所示:
public static BindableProperty ParentBindingContextProperty =
BindableProperty.Create(nameof(ParentBindingContext), typeof(object),
typeof(PostViewTemplate), null);
public object ParentBindingContext
{
get { return GetValue(ParentBindingContextProperty); }
set { SetValue(ParentBindingContextProperty, value); }
}
<view:PostViewTemplate ParentBindingContext="{Binding Source={x:Reference Home}, Path=BindingContext}"/>
<ContentView ... x:Name="PostView" ...>
<controls:AwesomeButton BindingContext="{Binding Source={x:Reference PostView}, Path=ParentBindingContext}" Command="{Binding OnTopicsPopupClicked}" CommandParameter="{Binding Topics}" Text="" TextColor="#09478e" BorderWidth="0" Margin="-5" BackgroundColor="Transparent" WidthRequest="36" FontSize="24"></controls:AwesomeButton>
关于Xamarin 表单从 View 模型绑定(bind)命令到 ListView 数据模板中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47156909/
我不知道该怎么做... function f1() { var x = 10; function f2(fx) { var x; x = 6;
早期绑定(bind)和后期绑定(bind)有什么区别? 最佳答案 简短的回答是,早期(或静态)绑定(bind)是指编译时绑定(bind),后期(或动态)绑定(bind)是指运行时绑定(bind)(例如
如何在 SwiftUI View 上使用 Binding(get: { }, set: { }) 自定义绑定(bind)与 @Binding 属性。我已成功使用此自定义绑定(bind)与 @State
我经常发现自己遇到问题,即控件的两个(相关)值被更新,并且两者都会触发昂贵的操作,或者控件可能会暂时处于不一致的状态。 例如,考虑一个数据绑定(bind),其中两个值 (x,y) 相互减去,最终结果用
我想通过我的 ViewModel 控制我的一个窗口的高度和宽度。 这看起来很简单。 但没有。它不起作用。 它检查 ViewModel 的 Width但不是 Height . 奇怪的是,如果我切换 W
UI5中一次性绑定(bind)和单向绑定(bind)有什么区别? 是否有任何用户特定的用例我会使用它们? 我无法从文档中获得太多信息。 最佳答案 单程 它的作用:单向数据流。模型数据的变化(例如通过
(define make (lambda (x) (lambda (y) (cons x (list y))))) (let ((x 7) (p (make 4))) (cons
尽管我或多或少地了解什么是语言绑定(bind),但我很难理解它们是如何工作的。 例如,谁能解释一下如何为 WinAPI 制作 Java 绑定(bind)? 最佳答案 如果您搜索 Foreign Fun
谁能解释为什么我可以重新绑定(bind)列表但不能+? (binding [list vector] (list 1 3)) (binding [list +] (list 1 3)) (bi
我真的很喜欢 Caliburn 和命名约定绑定(bind),我很惊讶 可见性与“CanNAME”约定用于保护 Action 的方式不同。 据我所知, BooleanToVisibilityConver
我了解动态绑定(bind)的实现方式以及静态绑定(bind)和动态绑定(bind)之间的区别,但我只是无法理解动态绑定(bind)的定义。基本上它是一种运行时绑定(bind)类型。 最佳答案 基本上,
http://jsfiddle.net/3NRsd/ var foo = $("div").bind("click", function() { $("div").animate({"hei
这个问题我快疯了...我有一个用户控件,它有一个用于插入操作的 FormView 和一个用于所有其他操作的 GridView。 在这两个控件中,我都有一个 DropDownList,如下所示: '
我有一个绑定(bind)到 ListBox 的地址的 ObservableCollection。然后在 ItemTemplate 中,我使用 {Binding .} 绑定(bind)到当前地址记录。这
如果我有以下简单的 js/knockout 代码: .js( View 模型): var image = ko.observable('http://placehold.it/300x150'); 看
我正在 aurelia 上开发一个自定义属性,让用户在输入文本区域时从列表中进行选择。例如,用法将是这样的: 正如您可能注意到的,auto-complete是属性。现在,当我想显示提示时,我想在自定
我正在使用 EventEmitter2作为我的应用程序内部的消息总线。现在我需要绑定(bind)和取消绑定(bind)一些事件处理程序。因为我也希望他们bind将它们添加到给定的上下文中,我最终得到以
我有以下函数调用: $(".selector").on("click", callback.bind(this, param1, param2)); 在我的回调函数中,我想使用绑定(bind)的 th
我目前正在试验新的编译绑定(bind),并且(再次)达到了我在拼图中遗漏了一个小问题:为什么我必须调用 Bindings.Update?直到现在,我还认为实现 INotifyPropertyChang
我正在阅读一本关于编写 JavaScript 框架的书,并找到了这段代码。但是我不明白它是如何工作的,尤其是 bind.bind 的用法?有人知道吗? var bind = Function.prot
我是一名优秀的程序员,十分优秀!