Xamarin - Local:PinchPanContainer 未找到
Xamarin - Local:PinchPanContainer was not found
我找不到我做错了什么。
我有 Picture.xaml 页面,第 <local:PinchPanContainer>
行给出错误 "the type 'local:PinchPanContainer' was not found ..... "
这两个文件都在 Views 文件夹中。
Picture.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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:GalShare.Views;assembly=GalShare"
x:Class="GalShare.Views.Picture">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Save to gallery" Order="Primary" Clicked="SaveToGal_Clicked"></ToolbarItem>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout>
<ActivityIndicator BindingContext="{x:Reference MyImage}" IsRunning="{Binding IsLoading}"/>
<local:PinchPanContainer>
<local:PinchPanContainer.Content HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Image x:Name="MyImage" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
</local:PinchPanContainer.Content>
</local:PinchPanContainer>
</StackLayout>
</ContentPage.Content>
PitchPanContainer.cs:
using System;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
namespace GalShare.Views
{
public class PinchPanContainer : ContentView
{
double currentScale = 1;
double startScale = 1;
double xOffset = 0;
double yOffset = 0;
bool blnDisableMove = false;
public PinchPanContainer()
{
var pinchGesture = new PinchGestureRecognizer();
pinchGesture.PinchUpdated += OnPinchUpdated;
GestureRecognizers.Add(pinchGesture);
var panGesture = new PanGestureRecognizer();
panGesture.PanUpdated += OnPanUpdated;
GestureRecognizers.Add(panGesture);
}
void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
if (Content.Scale == 1)
{
return;
}
switch (e.StatusType)
{
case GestureStatus.Running:
if (!blnDisableMove)
{
Content.TranslationX = Math.Max(Math.Min(0, xOffset + (e.TotalX * Scale)), -Math.Abs((Content.Width * Content.Scale) - Application.Current.MainPage.Width));
Content.TranslationY = Math.Max(Math.Min(0, yOffset + (e.TotalY * Scale)), -Math.Abs((Content.Height * Content.Scale) - Application.Current.MainPage.Height));
}
break;
case GestureStatus.Completed:
if (blnDisableMove)
{
blnDisableMove = false;
return;
}
// Store the translation applied during the pan
xOffset = Content.TranslationX;
yOffset = Content.TranslationY;
break;
}
}
void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
if (e.Status == GestureStatus.Started)
{
// Store the current scale factor applied to the wrapped user interface element,
// and zero the components for the center point of the translate transform.
startScale = Content.Scale;
Content.AnchorX = 0;
Content.AnchorY = 0;
blnDisableMove = true;
}
if (e.Status == GestureStatus.Running)
{
// Calculate the scale factor to be applied.
currentScale += (e.Scale - 1) * startScale;
currentScale = Math.Max(1, currentScale);
// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the X pixel coordinate.
double renderedX = Content.X + xOffset;
double deltaX = renderedX / Width;
double deltaWidth = Width / (Content.Width * startScale);
double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;
// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the Y pixel coordinate.
double renderedY = Content.Y + yOffset;
double deltaY = renderedY / Height;
double deltaHeight = Height / (Content.Height * startScale);
double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;
// Calculate the transformed element pixel coordinates.
double targetX = xOffset - (originX * Content.Width) * (currentScale - startScale);
double targetY = yOffset - (originY * Content.Height) * (currentScale - startScale);
// Apply translation based on the change in origin.
Content.TranslationX = targetX.Clamp(-Content.Width * (currentScale - 1), 0);
Content.TranslationY = targetY.Clamp(-Content.Height * (currentScale - 1), 0);
// Apply scale factor.
Content.Scale = currentScale;
blnDisableMove = true;
}
if (e.Status == GestureStatus.Completed)
{
// Store the translation delta's of the wrapped user interface element.
xOffset = Content.TranslationX;
yOffset = Content.TranslationY;
blnDisableMove = true;
}
}
}
}
我做错了什么?
它不起作用的原因是你没有正确使用它扩展选项不应该在内容上属性但是它上面的那一行你声明布局,下面是你的容器应该是什么样子
<local:PinchPanContainer HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Image x:Name="MyImage" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
</local:PinchPanContainer>
此外,请注意内容 属性 是视图的默认设置,因此它会直接将您视图的子项视为 属性 的值。
此外,您的命名空间不需要指定程序集,因为您正在同一个程序集中使用它!
xmlns:local="clr-namespace:GalShare.Views"
祝你好运
如果您有任何问题,请随时回来
我找不到我做错了什么。
我有 Picture.xaml 页面,第 <local:PinchPanContainer>
行给出错误 "the type 'local:PinchPanContainer' was not found ..... "
这两个文件都在 Views 文件夹中。
Picture.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:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:local="clr-namespace:GalShare.Views;assembly=GalShare"
x:Class="GalShare.Views.Picture">
<ContentPage.ToolbarItems>
<ToolbarItem Text="Save to gallery" Order="Primary" Clicked="SaveToGal_Clicked"></ToolbarItem>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout>
<ActivityIndicator BindingContext="{x:Reference MyImage}" IsRunning="{Binding IsLoading}"/>
<local:PinchPanContainer>
<local:PinchPanContainer.Content HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Image x:Name="MyImage" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
</local:PinchPanContainer.Content>
</local:PinchPanContainer>
</StackLayout>
</ContentPage.Content>
PitchPanContainer.cs:
using System;
using Xamarin.Forms;
using Xamarin.Forms.Internals;
namespace GalShare.Views
{
public class PinchPanContainer : ContentView
{
double currentScale = 1;
double startScale = 1;
double xOffset = 0;
double yOffset = 0;
bool blnDisableMove = false;
public PinchPanContainer()
{
var pinchGesture = new PinchGestureRecognizer();
pinchGesture.PinchUpdated += OnPinchUpdated;
GestureRecognizers.Add(pinchGesture);
var panGesture = new PanGestureRecognizer();
panGesture.PanUpdated += OnPanUpdated;
GestureRecognizers.Add(panGesture);
}
void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
if (Content.Scale == 1)
{
return;
}
switch (e.StatusType)
{
case GestureStatus.Running:
if (!blnDisableMove)
{
Content.TranslationX = Math.Max(Math.Min(0, xOffset + (e.TotalX * Scale)), -Math.Abs((Content.Width * Content.Scale) - Application.Current.MainPage.Width));
Content.TranslationY = Math.Max(Math.Min(0, yOffset + (e.TotalY * Scale)), -Math.Abs((Content.Height * Content.Scale) - Application.Current.MainPage.Height));
}
break;
case GestureStatus.Completed:
if (blnDisableMove)
{
blnDisableMove = false;
return;
}
// Store the translation applied during the pan
xOffset = Content.TranslationX;
yOffset = Content.TranslationY;
break;
}
}
void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
if (e.Status == GestureStatus.Started)
{
// Store the current scale factor applied to the wrapped user interface element,
// and zero the components for the center point of the translate transform.
startScale = Content.Scale;
Content.AnchorX = 0;
Content.AnchorY = 0;
blnDisableMove = true;
}
if (e.Status == GestureStatus.Running)
{
// Calculate the scale factor to be applied.
currentScale += (e.Scale - 1) * startScale;
currentScale = Math.Max(1, currentScale);
// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the X pixel coordinate.
double renderedX = Content.X + xOffset;
double deltaX = renderedX / Width;
double deltaWidth = Width / (Content.Width * startScale);
double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;
// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the Y pixel coordinate.
double renderedY = Content.Y + yOffset;
double deltaY = renderedY / Height;
double deltaHeight = Height / (Content.Height * startScale);
double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;
// Calculate the transformed element pixel coordinates.
double targetX = xOffset - (originX * Content.Width) * (currentScale - startScale);
double targetY = yOffset - (originY * Content.Height) * (currentScale - startScale);
// Apply translation based on the change in origin.
Content.TranslationX = targetX.Clamp(-Content.Width * (currentScale - 1), 0);
Content.TranslationY = targetY.Clamp(-Content.Height * (currentScale - 1), 0);
// Apply scale factor.
Content.Scale = currentScale;
blnDisableMove = true;
}
if (e.Status == GestureStatus.Completed)
{
// Store the translation delta's of the wrapped user interface element.
xOffset = Content.TranslationX;
yOffset = Content.TranslationY;
blnDisableMove = true;
}
}
}
}
我做错了什么?
它不起作用的原因是你没有正确使用它扩展选项不应该在内容上属性但是它上面的那一行你声明布局,下面是你的容器应该是什么样子
<local:PinchPanContainer HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Image x:Name="MyImage" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
</local:PinchPanContainer>
此外,请注意内容 属性 是视图的默认设置,因此它会直接将您视图的子项视为 属性 的值。
此外,您的命名空间不需要指定程序集,因为您正在同一个程序集中使用它!
xmlns:local="clr-namespace:GalShare.Views"
祝你好运
如果您有任何问题,请随时回来