XAML绑定智能感知在ListView中使用的单独ContentView文件中

时间:2017-11-23 08:54:01

标签: xaml xamarin.forms intellisense

我有一个带有此项目模板的ListView:

<ListView.ItemTemplate>
  <DataTemplate>
    <ViewCell>
      <views:ProjectListEntry />
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>

ProjectListEntry相当复杂,并在另一个ListView中使用,所以我将它放在自己的文件中。我把它设置成这样:

<ContentView
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    ...
    BindingContext="{x:Static vms:DesignTimeData.ProjectListEntryVm}">

如您所见,为了获得绑定的Intellisense(使用ReSharper),我尝试将BindingContext设置为DesignTimeData类上的静态成员。这适用于我的Pages(我在运行时替换BindingContext),但对于ListViews中使用的ContentViews,ContentView的BindingContext似乎是继承的(我猜想来自ViewCell)。这意味着我的ContentView上的显式BindingContext将实际覆盖ListView在ViewCell上设置的BindingContext,并且我的所有列表元素将在运行时反映静态设计时数据。但是,如果我删除BindingContext,我得不到我在ContentView文件中绑定的成员的智能感知。

是否有一种简单的方法可以在ContentView中获取Intellisense进行绑定?

(如上所述,我不能在ListView定义中内联ContentView,因为ContentView相当复杂并且在几个列表中使用。我也不能使用某种VM定位器,因为虽然我正在使用绑定,我没有使用“完整”的MVVM - 我正在使用类似Redux的架构。而且我认为VM定位器无论如何都不适用于上述情况,原因与上述不起作用相同。)

(从Xamarin Forums交叉发布,我没有收到任何回复。)

1 个答案:

答案 0 :(得分:0)

This solution似乎工作正常。简而言之:添加

public void sftp(File file) throws  SftpException, FileNotFoundException, JSchException
    {
        Session session = getSession();
/* Duplicate code START*/
        if ( session == null ) throw new SftpException(0 , "Service: Session is NULL"); 

        ChannelSftp channelSftp = null;

        Channel channel = session.openChannel("sftp");
        channel.connect();
        channelSftp = (ChannelSftp) channel;
/* Duplicate code END*/
        channelSftp.cd(VENDOR1_PATH);
        channelSftp.put(new FileInputStream(file), file.getName());

        channelSftp.disconnect();
        session.disconnect();
    }

并使用private void CheckBoxSetup_Click(object sender, RoutedEventArgs e) { if (CheckBoxSetup.IsChecked == false && CheckBoxUpdate.IsChecked == false) { MainWindow1.MinHeight = 50; OldWindowHeight = MainWindow1.Height; int NewWindowHeigth = 180; WindowDiff = NewWindowHeigth - OldWindowHeight; WindowDiff = WindowDiff / 30; for (int a = 0; a < 30; a++) { OldWindowHeight = OldWindowHeight + WindowDiff; MainWindow1.Height = OldWindowHeight; MainWindow1.MinHeight = OldWindowHeight; } } if (CheckBoxSetup.IsChecked == true || CheckBoxUpdate.IsChecked == true) { MainWindow1.MinHeight = 50; OldWindowHeight = MainWindow1.Height; int NewWindowHeigth = 220; WindowDiff = NewWindowHeigth - OldWindowHeight; WindowDiff = WindowDiff / 30; for (int a = 0; a < 30; a++) { OldWindowHeight = OldWindowHeight + WindowDiff; MainWindow1.Height = OldWindowHeight; MainWindow1.MinHeight = OldWindowHeight; } //MainWindow1.MinHeight = 220; } if (CheckBoxSetup.IsChecked == true && CheckBoxUpdate.IsChecked == true) { MainWindow1.MinHeight = 50; OldWindowHeight = MainWindow1.Height; int NewWindowHeigth = 260; WindowDiff = NewWindowHeigth - OldWindowHeight; WindowDiff = WindowDiff / 30; for (int a = 0; a < 30; a++) { OldWindowHeight = OldWindowHeight + WindowDiff; MainWindow1.Height = OldWindowHeight; MainWindow1.MinHeight = OldWindowHeight; } //MainWindow1.MinHeight = 260; } if (CheckBoxSetup.IsChecked == true) { LabelDestinationSetup.Visibility = Visibility.Visible; TextBoxDestinationpathSetup.Visibility = Visibility.Visible; CheckCountSetup++; } else { LabelDestinationSetup.Visibility = Visibility.Hidden; TextBoxDestinationpathSetup.Visibility = Visibility.Hidden; CheckCountSetup--; } if (CheckCountSetup == 1) { LabelDestinationSetup.Margin = new Thickness(24, thickTop1, 0, 0); TextBoxDestinationpathSetup.Margin = new Thickness(160, thickTop1, 0, 0); LabelDestinationUpdatepackage.Margin = new Thickness(24, thickTop2, 0, 0); TextBoxDestinationpathUpdatePackage.Margin = new Thickness(160, thickTop2, 0, 0); } if (CheckCountSetup == 0 && CheckCountUpdate == 1) { LabelDestinationSetup.Margin = new Thickness(24, thickTop2, 0, 0); TextBoxDestinationpathSetup.Margin = new Thickness(160, thickTop2, 0, 0); LabelDestinationUpdatepackage.Margin = new Thickness(24, thickTop1, 0, 0); TextBoxDestinationpathUpdatePackage.Margin = new Thickness(160, thickTop1, 0, 0); } if (CheckCountSetup == 0) { LabelDestinationSetup.Margin = new Thickness(24, thickTop2, 0, 0); TextBoxDestinationpathSetup.Margin = new Thickness(160, thickTop2, 0, 0); LabelDestinationUpdatepackage.Margin = new Thickness(24, thickTop1, 0, 0); TextBoxDestinationpathUpdatePackage.Margin = new Thickness(160, thickTop1, 0, 0); } } private void CheckBoxUpdate_Click(object sender, RoutedEventArgs e) { if(CheckBoxUpdate.IsChecked == true && CheckBoxFTP.IsChecked == false){ CheckBox64Bit.IsEnabled = true; }else if(CheckBoxUpdate.IsChecked == false){ CheckBox64Bit.IsEnabled = false; CheckBox64Bit.IsChecked = false; } if (CheckBoxSetup.IsChecked == false && CheckBoxUpdate.IsChecked == false) { MainWindow1.MinHeight = 50; OldWindowHeight = MainWindow1.Height; int NewWindowHeigth = 180; WindowDiff = NewWindowHeigth - OldWindowHeight; WindowDiff = WindowDiff / 30; for (int a = 0; a < 30; a++) { OldWindowHeight = OldWindowHeight + WindowDiff; MainWindow1.Height = OldWindowHeight; MainWindow1.MinHeight = OldWindowHeight; } //MainWindow1.MinHeight = 180; } if (CheckBoxSetup.IsChecked == true || CheckBoxUpdate.IsChecked == true) { MainWindow1.MinHeight = 50; OldWindowHeight = MainWindow1.Height; int NewWindowHeigth = 220; WindowDiff = NewWindowHeigth - OldWindowHeight; WindowDiff = WindowDiff / 30; for (int a = 0; a < 30; a++) { OldWindowHeight = OldWindowHeight + WindowDiff; MainWindow1.Height = OldWindowHeight; MainWindow1.MinHeight = OldWindowHeight; } //MainWindow1.MinHeight = 220; } if (CheckBoxSetup.IsChecked == true && CheckBoxUpdate.IsChecked == true) { MainWindow1.MinHeight = 50; OldWindowHeight = MainWindow1.Height; int NewWindowHeigth = 260; WindowDiff = NewWindowHeigth - OldWindowHeight; WindowDiff = WindowDiff / 30; for (int a = 0; a < 30; a++) { OldWindowHeight = OldWindowHeight + WindowDiff; MainWindow1.Height = OldWindowHeight; MainWindow1.MinHeight = OldWindowHeight; } //MainWindow1.MinHeight = 260; } if (CheckBoxUpdate.IsChecked == true) { LabelDestinationUpdatepackage.Visibility = Visibility.Visible; TextBoxDestinationpathUpdatePackage.Visibility = Visibility.Visible; CheckCountUpdate++; } else { LabelDestinationUpdatepackage.Visibility = Visibility.Hidden; TextBoxDestinationpathUpdatePackage.Visibility = Visibility.Hidden; CheckCountUpdate--; } if (CheckCountUpdate == 1) { LabelDestinationUpdatepackage.Margin = new Thickness(24, thickTop1, 0, 0); TextBoxDestinationpathUpdatePackage.Margin = new Thickness(160, thickTop1, 0, 0); LabelDestinationSetup.Margin = new Thickness(24, thickTop2, 0, 0); TextBoxDestinationpathSetup.Margin = new Thickness(160, thickTop2, 0, 0); } if (CheckCountSetup == 1 && CheckCountUpdate == 0) { LabelDestinationUpdatepackage.Margin = new Thickness(24, thickTop2, 0, 0); TextBoxDestinationpathUpdatePackage.Margin = new Thickness(160, thickTop2, 0, 0); LabelDestinationSetup.Margin = new Thickness(24, thickTop1, 0, 0); TextBoxDestinationpathSetup.Margin = new Thickness(160, thickTop1, 0, 0); } if (CheckCountUpdate == 0) { LabelDestinationUpdatepackage.Margin = new Thickness(24, thickTop2, 0, 0); TextBoxDestinationpathUpdatePackage.Margin = new Thickness(181, thickTop2, 0, 0); LabelDestinationSetup.Margin = new Thickness(24, thickTop1, 0, 0); TextBoxDestinationpathSetup.Margin = new Thickness(160, thickTop1, 0, 0); } } 代替xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 。从上面的例子开始,它应该是这样的:

d:DataContext

这导致来自ReSharper的工作绑定智能感知,并且在运行时不会引起问题。